<!DOCTYPE HTML>
<html lang="zh-CN">


<head>
    <meta charset="utf-8">
    <meta name="keywords" content="我的笔记, 博客 blog">
    <meta name="description" content="熊猫小二的博客  xmxe&#39;s blog">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
    <meta name="mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <!-- 为了引用qq空间图床文件 -->
    <meta name="referrer" content="no-referrer">
    <!-- Global site tag (gtag.js) - Google Analytics -->


    <title>我的笔记 | 熊猫小二</title>
    <link rel="icon" type="image/x-icon, image/vnd.microsoft.icon" href="/blog/favicon.ico">
    <link rel="stylesheet" type="text/css" href="/blog/libs/awesome/css/all.css">
    <link rel="stylesheet" type="text/css" href="/blog/libs/materialize/materialize.min.css">
    <link rel="stylesheet" type="text/css" href="/blog/libs/aos/aos.css">
    <link rel="stylesheet" type="text/css" href="/blog/libs/animate/animate.min.css">
    <link rel="stylesheet" type="text/css" href="/blog/libs/lightGallery/css/lightgallery.min.css">
    <link rel="stylesheet" type="text/css" href="/blog/css/matery.css">
    <link rel="stylesheet" type="text/css" href="/blog/css/my.css">
    <link rel="stylesheet" type="text/css" href="/blog/css/loading.css">

    <script src="/blog/libs/jquery/jquery.min.js"></script>

<meta name="generator" content="Hexo 6.3.0"></head>



   
<style>
    body{
       background-image: url(/blog/medias/cover.jpg);
       background-repeat:no-repeat;
       background-size:cover;
       background-attachment:fixed;
    }
</style>



<body>
    
  <div id="loading-box">
    <div class="loading-left-bg"></div>
    <div class="loading-right-bg"></div>
    <div class="spinner-box">
      <div class="configure-border-1">
        <div class="configure-core"></div>
      </div>
      <div class="configure-border-2">
        <div class="configure-core"></div>
      </div>
      <div class="loading-word">加载中...</div>
    </div>
  </div>
  <!-- 页面加载动画 -->
  <script>
    $(document).ready(function () {
      // document.body.style.overflow = 'auto';
      document.getElementById('loading-box').classList.add("loaded")
    })
  </script>

    <header class="navbar-fixed">
    <nav id="headNav" class="bg-color nav-transparent">
        <div id="navContainer" class="nav-wrapper container">
            <div class="brand-logo">
                <a href="/blog/" class="waves-effect waves-light">
                    
                        <img src="/blog/medias/logo.png" class="logo-img" alt="LOGO">
                    
                    <span class="logo-span">熊猫小二</span>
                </a>
            </div>
            

<a href="#" data-target="mobile-nav" class="sidenav-trigger button-collapse"><i class="fas fa-bars"></i></a>
<ul class="right nav-menu">
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/blog/" class="waves-effect waves-light">
      
      <i class="fas fa-home" style="zoom: 0.6;"></i>
      
      <span>首页</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="#" class="waves-effect waves-light">

      
      <i class="fas fa-book" style="zoom: 0.6;"></i>
      
      <span>归档</span>
      <i class="fas fa-chevron-down" aria-hidden="true" style="zoom: 0.6;"></i>
    </a>
    <ul class="sub-nav menus_item_child ">
      
      <li>
        <a href="/blog/archives">
          
          <i class="fas fa-archive" style="margin-top: -20px; zoom: 0.6;"></i>
          
          <span>归档</span>
        </a>
      </li>
      
      <li>
        <a href="/blog/tags">
          
          <i class="fas fa-tags" style="margin-top: -20px; zoom: 0.6;"></i>
          
          <span>标签</span>
        </a>
      </li>
      
      <li>
        <a href="/blog/categories">
          
          <i class="fas fa-bookmark" style="margin-top: -20px; zoom: 0.6;"></i>
          
          <span>分类</span>
        </a>
      </li>
      
    </ul>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/blog/friends" class="waves-effect waves-light">
      
      <i class="fas fa-address-book" style="zoom: 0.6;"></i>
      
      <span>友链</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="#" class="waves-effect waves-light">

      
      <i class="fas fa-user-circle" style="zoom: 0.6;"></i>
      
      <span>关于</span>
      <i class="fas fa-chevron-down" aria-hidden="true" style="zoom: 0.6;"></i>
    </a>
    <ul class="sub-nav menus_item_child ">
      
      <li>
        <a href="/blog/about">
          
          <i class="fas fa-star-of-david" style="margin-top: -20px; zoom: 0.6;"></i>
          
          <span>主页</span>
        </a>
      </li>
      
      <li>
        <a href="/blog/gallery">
          
          <i class="fas fa-images" style="margin-top: -20px; zoom: 0.6;"></i>
          
          <span>相册</span>
        </a>
      </li>
      
    </ul>
    
  </li>
  
  <li>
    <a href="#searchModal" class="modal-trigger waves-effect waves-light">
      <i id="searchIcon" class="fas fa-search" title="搜索" style="zoom: 0.85;"></i>
    </a>
  </li>
  
    <li>
      <a class="waves-effect waves-light" onclick="switchNightMode()">
        <i id="sum-moon-icon" class="fas fa-sun" style="zoom:0.65;" title="切换主题"></i>
      </a>
    </li>
  
  
</ul>


<div id="mobile-nav" class="side-nav sidenav">

    <div class="mobile-head bg-color">
        
          <img src="/blog/medias/logo.png" class="logo-img circle responsive-img">
        
        <div class="logo-name">熊猫小二</div>
        <div class="logo-desc">
            
            熊猫小二的博客  xmxe&#39;s blog
            
        </div>
    </div>

    

    <ul class="menu-list mobile-menu-list">
        
        <li class="m-nav-item">
	  
		<a href="/blog/" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-home"></i>
			
			首页
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="javascript:;">
			
				<i class="fa-fw fas fa-book"></i>
			
			归档
			<span class="m-icon"><i class="fas fa-chevron-right"></i></span>
		</a>
            <ul  >
              
                <li>

                  <a href="/blog/archives " style="margin-left:75px">
				  
				   <i class="fa fas fa-archive" style="position: absolute;left:50px" ></i>
			      
		          <span>归档</span>
                  </a>
                </li>
              
                <li>

                  <a href="/blog/tags " style="margin-left:75px">
				  
				   <i class="fa fas fa-tags" style="position: absolute;left:50px" ></i>
			      
		          <span>标签</span>
                  </a>
                </li>
              
                <li>

                  <a href="/blog/categories " style="margin-left:75px">
				  
				   <i class="fa fas fa-bookmark" style="position: absolute;left:50px" ></i>
			      
		          <span>分类</span>
                  </a>
                </li>
              
            </ul>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/blog/friends" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-address-book"></i>
			
			友链
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="javascript:;">
			
				<i class="fa-fw fas fa-user-circle"></i>
			
			关于
			<span class="m-icon"><i class="fas fa-chevron-right"></i></span>
		</a>
            <ul  >
              
                <li>

                  <a href="/blog/about " style="margin-left:75px">
				  
				   <i class="fa fas fa-star-of-david" style="position: absolute;left:50px" ></i>
			      
		          <span>主页</span>
                  </a>
                </li>
              
                <li>

                  <a href="/blog/gallery " style="margin-left:75px">
				  
				   <i class="fa fas fa-images" style="position: absolute;left:50px" ></i>
			      
		          <span>相册</span>
                  </a>
                </li>
              
            </ul>
          
        </li>
        
        
    </ul>
</div>


        </div>

        
    </nav>

</header>

    
<script src="/blog/libs/cryptojs/crypto-js.min.js"></script>
<script>
    (function() {
        let pwd = '';
        if (pwd && pwd.length > 0) {
            if (pwd !== CryptoJS.SHA256(prompt('请输入访问本文章的密码')).toString(CryptoJS.enc.Hex)) {
                alert('密码错误，将返回主页！');
                location.href = '/blog/';
            }
        }
    })();
</script>




<div class="bg-cover pd-header post-cover" style="background-image: url('/blog/medias/featureimages/13.jpg')">
    <div class="container" style="right: 0px;left: 0px;">
        <div class="row">
            <div class="col s12 m12 l12">
                <div class="brand">
                    <h1 id="post-title" class="description center-align post-title"></h1>

                    
                        <!-- <script src="https://cdn.jsdelivr.net/npm/typed.js@2.0.11"></script> -->
                        <script>
                            var typedObj = new Typed("#post-title", {
                                strings: [ '我的笔记' ],
                                startDelay: 300,
                                typeSpeed: 70,
                                loop: false,
                                backSpeed: 50,
                                showCursor: true
                            });
                        </script>
                    
                </div>
            </div>
        </div>
    </div>
</div>




<main class="post-container content">

    
    <link rel="stylesheet" href="/blog/libs/tocbot/tocbot.css">
<style>
    #articleContent h1::before,
    #articleContent h2::before,
    #articleContent h3::before,
    #articleContent h4::before,
    #articleContent h5::before,
    #articleContent h6::before {
        display: block;
        content: " ";
        height: 100px;
        margin-top: -100px;
        visibility: hidden;
    }

    #articleContent :focus {
        outline: none;
    }

    .toc-fixed {
        position: fixed;
        top: 64px;
    }

    .toc-widget {
        width: 345px;
        padding-left: 20px;
        background-color: rgb(255, 255, 255,0.7);
        border-radius: 10px;
        box-shadow: 0 10px 35px 2px rgba(0, 0, 0, .15), 0 5px 15px rgba(0, 0, 0, .07), 0 2px 5px -5px rgba(0, 0, 0, .1) !important;
    }

    .toc-widget .toc-title {
        padding: 35px 0 15px 17px;
        font-size: 1.5rem;
        font-weight: bold;
        line-height: 1.5rem;
    }

    .toc-widget ol {
        padding: 0;
        list-style: none;
    }

    #toc-content {
        padding-bottom: 30px;
        overflow: auto;
        max-height: 480px;
    }

    #toc-content ol {
        padding-left: 10px;
    }

    #toc-content ol li {
        padding-left: 10px;
    }

    #toc-content .toc-link:hover {
        color: #42b983;
        font-weight: 700;
        text-decoration: underline;
    }

    #toc-content .toc-link::before {
        background-color: transparent;
        max-height: 25px;

        position: absolute;
        right: 23.5vw;
        display: block;
    }

    #toc-content .is-active-link {
        color: #42b983;
    }

    #floating-toc-btn {
        position: fixed;
        right: 15px;
        bottom: 76px;
        padding-top: 15px;
        margin-bottom: 0;
        z-index: 998;
    }

    #floating-toc-btn .btn-floating {
        width: 48px;
        height: 48px;
    }

    #floating-toc-btn .btn-floating i {
        line-height: 48px;
        font-size: 1.4rem;
    }
</style>
<div class="row">
    <div id="main-content" class="col s12 m12 l9">
        <!-- 文章内容详情 -->
<div id="artDetail">
    <div class="card">
        <div class="card-content article-info">
            <div class="row tag-cate">
                <div class="col s7">
                    
                    <div class="article-tag">
                        
                            <a href="/blog/tags/%E9%9A%8F%E7%AC%94/">
                                <span class="chip bg-color">随笔</span>
                            </a>
                        
                    </div>
                    
                </div>
                <div class="col s5 right-align">
                    
                </div>
            </div>

            <div class="post-info">
                

                

                

                

                
            </div>
        </div>
        <hr class="clearfix">

        
        <!-- 是否加载使用自带的 prismjs. -->
        <link rel="stylesheet" href="/blog/libs/prism/prism.css">
        

        
        <!-- 代码块折行 -->
        <style type="text/css">
            code[class*="language-"], pre[class*="language-"] { white-space: pre-wrap !important; }
        </style>
        

        <div class="card-content article-card-content">
            <div id="articleContent">
                <h2 id="笔记"><a href="#笔记" class="headerlink" title="笔记"></a>笔记</h2><h3 id="代码块"><a href="#代码块" class="headerlink" title="代码块"></a>代码块</h3><h4 id="静态代码块"><a href="#静态代码块" class="headerlink" title="静态代码块"></a>静态代码块</h4><p>在类被加载的时候就运行了，而且只运行一次，并且优先于各种代码块以及构造函数。如果一个类中有多个静态代码块，会按照书写顺序依次执行。静态代码块不能存在任何方法体中静态代码块不能访问普通变量</p>
<h4 id="构造代码块"><a href="#构造代码块" class="headerlink" title="构造代码块"></a>构造代码块</h4><p>在创建对象时被调用，每次创建对象都会调用一次，但是优先于构造函数执行。需要注意的是，构造代码块不是优先于构造函数执行，而是依托于构造函数，也就是说，如果你不实例化对象，构造代码块是不会执行的</p>
<h4 id="普通代码块"><a href="#普通代码块" class="headerlink" title="普通代码块"></a>普通代码块</h4><p>和构造代码块的区别是，构造代码块是在类中定义的，而普通代码块是在方法体中定义的。且普通代码块的执行顺序和书写顺序一致</p>
<p>静态代码块&gt;构造代码块（类中定义）&gt;构造函数&gt;普通代码块(方法中定义)</p>
<h3 id="内部类"><a href="#内部类" class="headerlink" title="内部类"></a>内部类</h3><h4 id="成员内部类"><a href="#成员内部类" class="headerlink" title="成员内部类"></a>成员内部类</h4><p>也是最普通的内部类，它是外围类的一个成员，所以他是可以无限制的访问外围类的所有成员属性和方法，尽管是private的，但是外围类要访问内部类的成员属性和方法则需要通过内部类实例来访问。<br>在成员内部类中要注意两点，第一：成员内部类中不能存在任何static的变量和方法；第二：成员内部类是依附于外围类的，所以只有先创建了外围类才能够创建内部类。</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">OuterClass</span> outerClass <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">OuterClass</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">OuterClass<span class="token punctuation">.</span>InnerClass</span> innerClass <span class="token operator">=</span> outerClass<span class="token punctuation">.</span><span class="token keyword">new</span> <span class="token class-name">InnerClass</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
innerClass<span class="token punctuation">.</span><span class="token function">getOuterClass</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">display</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>

<h4 id="局部内部类"><a href="#局部内部类" class="headerlink" title="局部内部类"></a>局部内部类</h4><p>有这样一种内部类，它是嵌套在方法和作用域内的，对于这个类的使用主要是应用与解决比较复杂的问题，想创建一个类来辅助我们的解决方案，到那时又不希望这个类是公共可用的，所以就产生了局部内部类，局部内部类和成员内部类一样被编译，只是它的作用域发生了改变，它只能在该方法和属性中被使用，出了该方法和属性就会失效。</p>
<h4 id="静态内部类"><a href="#静态内部类" class="headerlink" title="静态内部类"></a>静态内部类</h4><p>使用static修饰的内部类我们称之为静态内部类，不过我们更喜欢称之为嵌套内部类。静态内部类与非静态内部类之间存在一个最大的区别，我们知道非静态内部类在编译完成之后会隐含地保存着一个引用，该引用是指向创建它的外围内，但是静态内部类却没有。没有这个引用就意味着：</p>
<ol>
<li>它的创建是不需要依赖于外围类的。</li>
<li>它不能使用任何外围类的非static成员变量和方法。</li>
<li>在静态内部类中可以存在静态成员</li>
<li>静态内部类只能访问外围类的静态成员变量和方法，不能访问外围类的非静态成员变量和方法</li>
<li>非静态内部类中不能存在静态成员，非静态内部类中可以调用外围类的任何成员,不管是静态的还是非静态的</li>
<li>静态内部类可以直接创建实例不需要依赖于外围类</li>
<li>非静态内部的创建需要依赖于外围类方位</li>
<li>非静态内部类的成员需要使用非静态内部类的实例new</li>
</ol>
<h4 id="匿名内部类"><a href="#匿名内部类" class="headerlink" title="匿名内部类"></a>匿名内部类</h4><p>new父类构造器（参数列表）|实现接口（）{<br>   &#x2F;&#x2F;匿名内部类的类体部分<br>}<br>在这里我们看到使用匿名内部类我们必须要继承一个父类或者实现一个接口，当然也仅能只继承一个父类或者实现一个接口。同时它也是没有class关键字，这是因为匿名内部类是直接使用new来生成一个对象的引用。当然这个引用是隐式的。</p>
<ol>
<li>使用匿名内部类时，我们必须是继承一个类或者实现一个接口，但是两者不可兼得，同时也只能继承一个类或者实现一个接口。</li>
<li>匿名内部类中是不能定义构造函数的。</li>
<li>匿名内部类中不能存在任何的静态成员变量和静态方法。</li>
<li>匿名内部类为局部内部类，所以局部内部类的所有限制同样对匿名内部类生效。</li>
<li>匿名内部类不能是抽象的，它必须要实现继承的类或者实现的接口的所有抽象方法。</li>
</ol>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">Thread</span> thread <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Thread</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">Runnable</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
	<span class="token annotation punctuation">@Override</span>
	<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">run</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
	<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>title<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
 <span class="token punctuation">&#125;</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h4 id="使用内部类的优势"><a href="#使用内部类的优势" class="headerlink" title="使用内部类的优势"></a>使用内部类的优势</h4><p>封装性、实现多继承※、用匿名内部类实现回调功能、解决继承及实现接口出现同名方法的问题</p>
<table>
<thead>
<tr>
<th align="center"><a target="_blank" rel="noopener" href="https://www.cnblogs.com/chenssy/p/3388487.html">详解内部类</a></th>
<th align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/Is2Ka2R_PWZP3XZsUroLIg">深入理解Java：内部类</a></th>
<th align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/u8rgIwoxnXLe8GPMrFbXpQ">Java内部类有坑,100%内存泄露！</a></th>
</tr>
</thead>
</table>
<h3 id="元注解"><a href="#元注解" class="headerlink" title="元注解"></a>元注解</h3><h4 id="Retention"><a href="#Retention" class="headerlink" title="@Retention"></a>@Retention</h4><p>表示需要在什么级别保存该注释信息，用于描述注解的生命周期（即：被描述的注解在什么范围内有效）,取值（RetentionPoicy）有：</p>
<ol>
<li>SOURCE:在源文件中有效（即源文件保留）</li>
<li>CLASS:在class文件中有效（即class保留）</li>
<li>RUNTIME:在运行时有效（即运行时保留）</li>
</ol>
<h4 id="Target"><a href="#Target" class="headerlink" title="@Target"></a>@Target</h4><p>用于描述注解的使用范围（即：被描述的注解可以用在什么地方）,取值(ElementType)有：</p>
<ol>
<li>CONSTRUCTOR:用于描述构造器</li>
<li>FIELD:用于描述域</li>
<li>LOCAL_VARIABLE:用于描述局部变量</li>
<li>METHOD:用于描述方法</li>
<li>PACKAGE:用于描述包</li>
<li>PARAMETER:用于描述参数</li>
<li>TYPE:用于描述类、接口(包括注解类型)或enum声明</li>
<li>ANNOTATION_TYPE:标明注解可以用于注解声明(应用于另一个注解上)</li>
<li>TYPE_PARAMETER:表示该注解能写在类型参数的声明语句中也就是泛型上，类型参数声明如下：&lt;T&gt;，&lt; T extends Person&gt;</li>
<li>TYPE_USE:表示注解可以在任何用到该类的地方使用</li>
</ol>
<h4 id="Inherited"><a href="#Inherited" class="headerlink" title="@Inherited"></a>@Inherited</h4><p>标记这个注解是继承于哪个注解类(默认注解并没有继承于任何子类)</p>
<h4 id="Documented"><a href="#Documented" class="headerlink" title="@Documented"></a>@Documented</h4><p>标记这些注解是否包含在用户文档中。</p>
<h4 id="Repeatable"><a href="#Repeatable" class="headerlink" title="@Repeatable"></a>@Repeatable</h4><p>重复注解,在java8中新增了一个方法getAnnotationsByType，用于获取可重复的注解,返回类型是数组</p>
<table>
<thead>
<tr>
<th align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/kx_111lekaIzkYYYAwjwxg">你知道Java中的注解是如何工作的？</a></th>
<th align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s?__biz=Mzg2MDYzODI5Nw==&amp;mid=2247493932&amp;idx=1&amp;sn=c8e75d1f75467f240ad749853eae4668&amp;source=41#wechat_redirect">深入理解Java：注解</a></th>
<th align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s?__biz=Mzg2MDYzODI5Nw==&amp;mid=2247494076&amp;idx=2&amp;sn=ba12e71724444dfd8ce10297778682aa&amp;source=41#wechat_redirect">JDK中注解的底层原来是这样实现的</a></th>
</tr>
</thead>
<tbody><tr>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/Ggw-uPBmxZ0VDyACreMNOw">原来注解是这么实现的啊</a></td>
<td align="center"></td>
<td align="center"></td>
</tr>
</tbody></table>
<h3 id="构建工具"><a href="#构建工具" class="headerlink" title="构建工具"></a>构建工具</h3><h4 id="Maven"><a href="#Maven" class="headerlink" title="Maven"></a>Maven</h4><p><strong>常用命令</strong></p>
<ul>
<li><p>mvn package、install、deploy区别<br><strong>package</strong>命令完成了项目编译、单元测试、打包功能，但没有把打好的可执行jar包（war包或其它形式的包）布署到本地maven仓库和远程maven私服仓库<br><strong>install</strong>命令完成了项目编译、单元测试、打包功能，同时把打好的可执行jar包（war包或其它形式的包）布署到本地maven仓库，但没有布署到远程maven私服仓库<br><strong>deploy</strong>命令完成了项目编译、单元测试、打包功能，同时把打好的可执行jar包（war包或其它形式的包）布署到本地maven仓库和远程maven私服仓库</p>
</li>
<li><p>compile(编译 打包)<br>compile是默认的范围；如果没有提供一个范围，那该依赖的范围就是编译范围。编译范围依赖在所有的classpath中可用，同时它们也会被打包。</p>
</li>
<li><p>provided(已提供 编译不打包)<br>provided意味着打包的时候可以不用包进去，别的设施(Web Container)会提供。事实上该依赖理论上可以参与编译，测试，运行等周期。相当于compile，但是在打包阶段做了exclude的动作。</p>
</li>
<li><p>runtime(运行时 不编译打包)<br>runtime表示被依赖项目无需参与项目的编译，不过后期的测试和运行周期需要其参与。与compile相比，跳过编译而已。</p>
</li>
<li><p>test (测试)<br>test范围依赖在一般的编译和运行时都不需要，它们只有在测试编译和测试运行阶段可用。</p>
</li>
<li><p>system (系统)<br>Maven不会在仓库中去寻找它。如果你将一个依赖范围设置成系统范围，你必须同时提供一个systemPath元素。注意该范围是不推荐使用的（建议尽量去从公共或定制的Maven仓库中引用依赖）</p>
<pre class="line-numbers language-markup" data-language="markup"><code class="language-markup"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>com.example<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>local-jar-name<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>version</span><span class="token punctuation">></span></span>1.0.0<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>version</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>scope</span><span class="token punctuation">></span></span>system<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>scope</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>systemPath</span><span class="token punctuation">></span></span>$&#123;project.basedir&#125;/lib/local-jar-name.jar<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>systemPath</span><span class="token punctuation">></span></span>
    <span class="token comment">&lt;!-- $&#123;pom.basedir&#125;:表示pom文件的目录 --></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">></span></span>
<span class="token comment">&lt;!-- springboot打jar包时将本地jar也添加进去 --></span>
 <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>plugin</span><span class="token punctuation">></span></span>
     <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>org.springframework.boot<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span>
     <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>spring-boot-maven-plugin<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span>
     <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>configuration</span><span class="token punctuation">></span></span>
         <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>includeSystemScope</span><span class="token punctuation">></span></span>true<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>includeSystemScope</span><span class="token punctuation">></span></span>
     <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>configuration</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>plugin</span><span class="token punctuation">></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
</li>
<li><p>import(导入)<br>import仅支持在&lt;dependencyManagement&gt;中的类型依赖项上。它表示要在指定的POM&lt;dependencyManagement&gt;部分中用有效的依赖关系列表替换的依赖关系。该scope类型的依赖项实际上不会参与限制依赖项的可传递性。</p>
</li>
<li><p>&lt;optional&gt;标签<br>projectA依赖projectB,projectB依赖projectC时,当projectA在maven引入projectB时,如果projectB写上&lt;optional&gt;true&lt;&#x2F;optional&gt;时,则projectA不依赖projectC,即projectA可以自己选择是否依赖projectC,如果不写或者是false的时候,则projectA引入projectB时,也会引入projectC,默认&lt;optional&gt;的值为false,即子项目必须依赖.但是像引入parent继承情况时,像这样</p>
<pre class="line-numbers language-markup" data-language="markup"><code class="language-markup"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependencyManagement</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependencies</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>joda-time<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>joda-time<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>version</span><span class="token punctuation">></span></span>2.9.9<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>version</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>optional</span><span class="token punctuation">></span></span>true<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>optional</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependencies</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependencyManagement</span><span class="token punctuation">></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p>optional选项在统一控制版本的情况下会失效</p>
</li>
</ul>
<p><strong>Maven打包跳过测试的5种方法</strong></p>
<p>(1) 命令行方式</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token comment"># -DskipTests=true,不执行测试用例,但编译测试用例类生成相应的class文件至target/test-classes下。</span>
mvn package <span class="token parameter variable">-DskipTests</span><span class="token operator">=</span>true
<span class="token comment"># -Dmaven.test.skip=true,不执行测试用例,也不编译测试用例类。</span>
mvn package <span class="token parameter variable">-Dmaven.test.skip</span><span class="token operator">=</span>true<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre>
<p>(2) pom.xml</p>
<pre class="line-numbers language-markup" data-language="markup"><code class="language-markup"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>build</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>plugins</span><span class="token punctuation">></span></span>
        <span class="token comment">&lt;!-- maven打包时跳过测试 --></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>plugin</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>org.apache.maven.plugins<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>maven-surefire-plugin<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>configuration</span><span class="token punctuation">></span></span>
                <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>skip</span><span class="token punctuation">></span></span>true<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>skip</span><span class="token punctuation">></span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>configuration</span><span class="token punctuation">></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>plugin</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>plugins</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>build</span><span class="token punctuation">></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p>(3) IDEA图标Skip Tests。点击选中，再用LifeCycle中的打包就会跳过测试<br>(4) 打开配置，找到Build,Exxcution,Deployment–&gt;Build Tools–&gt;Maven–&gt;Runner，在VM option中添加-Dmaven.test.skip&#x3D;true或者-DskipTests&#x3D;true，就能在打包时跳过测试。<br>(5) 打开配置，找到Build,Exxcution,Deployment–&gt;Build Tools–&gt;Maven–&gt;Runner，在Properties中勾选Skip Test选项。</p>
<table>
<thead>
<tr>
<th align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/JD2wfLGXdujoc9DOJEZFpA">说一个大家都知道的Spring Boot小细节</a></th>
<th align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/Kt51X_VEeC_EQSw22pqB-A">Maven(五)：resources、profiles标签的实践</a></th>
<th align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/dEmCRy_9CtRPtOP4DViDHw">Maven最全教程，看了必懂，99%的人都收藏了！</a></th>
</tr>
</thead>
<tbody><tr>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/vq6Id8g0IBRVM3xdPRYFbA">如何利用Maven将代码打包成第三方公共jar包？</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/ctjJBNHiflsTr5O4SffvHQ">Maven中的classifier属性用过没？</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://blog.csdn.net/qq_43842093/article/details/131015256">Maven配置中repositories、distributionManagement、pluginRepositories详解&amp;将已有jar包部署到私服</a></td>
</tr>
</tbody></table>
<h4 id="Gradle"><a href="#Gradle" class="headerlink" title="Gradle"></a>Gradle</h4><ul>
<li><p>implementation，默认的scope,取代compile（已弃用）。implementation的作用域会让依赖在编译和运行时均包含在内，但是不会暴露在类库使用者的编译时。举例，如果我们的类库包含了gson，那么其他人使用我们的类库时，编译时不会出现gson的依赖。</p>
</li>
<li><p>api,和implementation类似，都是编译和运行时都可见的依赖。但是api允许我们将自己类库的依赖暴露给我们类库的使用者。</p>
</li>
<li><p>compileOnly和runtimeOnly，这两种顾名思义，一种只在编译时可见，一种只在运行时可见。而runtimeOnly和Maven的provided比较接近。runtimeOnly取代了runtime（不建议使用）</p>
</li>
<li><p>testImplementation，这种依赖在测试编译时和运行时可见，类似于Maven的test作用域。</p>
</li>
<li><p>testCompileOnly和testRuntimeOnly，这两种类似于compileOnly和runtimeOnly，但是作用于测试编译时和运行时。</p>
</li>
<li><p>compileClasspath延伸compile,compileOnly,implementation。编译类路径，在编译源代码时使用。由任务使用compileJava。runtimeClasspath延伸runtimeOnly,runtime,implementation，运行时类路径包含实现的元素以及仅运行时元素。</p>
</li>
<li><p>annotationProcessor编译期间使用的注释处理器。</p>
</li>
</ul>
<h3 id="String相关"><a href="#String相关" class="headerlink" title="String相关"></a>String相关</h3><ul>
<li><p>String.intern()是一个Native方法，底层调用C++的StringTable::intern方法实现。当通过语句str.intern()调用intern()方法后，JVM就会在当前类的常量池中查找是否存在与str等值的String，若存在则直接返回常量池中相应Strnig的引用；若不存在，则会在常量池中创建一个等值的String，然后返回这个String在常量池中的引用,通俗的讲，是将字符串放入常量池中。是一种手动将字符串加入常量池中的方法</p>
</li>
<li><p>String.toUpperCase()会在内存中新建一个字符串并不会修改原来的字符串</p>
</li>
<li><p>String对象new出来的字符串是放在堆中，直接赋值的字符串是放在常量池中的。<br>对字符串做拼接操作，即做“+”运算，分两种情况：<br>①表达式右边是纯字符串常量，则存放在常量池中<br>②表达式右边存在字符串引用，则存放在堆中</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">String</span> a <span class="token operator">=</span> <span class="token string">"s"</span><span class="token punctuation">;</span> <span class="token class-name">String</span> b <span class="token operator">=</span> <span class="token string">"b"</span><span class="token punctuation">;</span>
<span class="token class-name">String</span> c <span class="token operator">=</span> a<span class="token operator">+</span>b<span class="token punctuation">;</span><span class="token comment">//变量形式相加底层new String()不放常量池</span>
<span class="token class-name">String</span> d <span class="token operator">=</span> <span class="token string">"s"</span><span class="token operator">+</span><span class="token string">"b"</span><span class="token punctuation">;</span><span class="token comment">//非变量形式相加放到常量池</span>
<span class="token class-name">String</span> e <span class="token operator">=</span> <span class="token string">"sb"</span><span class="token punctuation">;</span><span class="token comment">//放到常量池</span>
<span class="token class-name">String</span> f <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">String</span><span class="token punctuation">(</span><span class="token string">"sb"</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//不放常量池</span>
<span class="token class-name">String</span> g <span class="token operator">=</span> f<span class="token punctuation">.</span><span class="token function">intern</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//手动放到常量池</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>d<span class="token operator">==</span>e<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//true</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>c<span class="token operator">==</span>e<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//false</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>c<span class="token operator">==</span>f<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//false</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>c<span class="token operator">==</span>d<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//false</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>e<span class="token operator">==</span>f<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//false</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>e<span class="token operator">==</span>g<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//true</span>

<span class="token class-name">String</span> s1 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">String</span><span class="token punctuation">(</span><span class="token string">"计算机"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">String</span> s2 <span class="token operator">=</span> s1<span class="token punctuation">.</span><span class="token function">intern</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">String</span> s3 <span class="token operator">=</span> <span class="token string">"计算机"</span><span class="token punctuation">;</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>s2<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//计算机</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>s1 <span class="token operator">==</span> s2<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//false，因为一个是堆内存中的String对象一个是常量池中的String对象，</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>s3 <span class="token operator">==</span> s2<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//true，因为两个都是常量池中的String对</span>

<span class="token class-name">String</span> str1 <span class="token operator">=</span> <span class="token string">"str"</span><span class="token punctuation">;</span>
<span class="token class-name">String</span> str2 <span class="token operator">=</span> <span class="token string">"ing"</span><span class="token punctuation">;</span>
<span class="token class-name">String</span> str3 <span class="token operator">=</span> <span class="token string">"str"</span> <span class="token operator">+</span> <span class="token string">"ing"</span><span class="token punctuation">;</span><span class="token comment">//常量池中的对象</span>
<span class="token class-name">String</span> str4 <span class="token operator">=</span> str1 <span class="token operator">+</span> str2<span class="token punctuation">;</span> <span class="token comment">//在堆上创建的新的对象</span>
<span class="token class-name">String</span> str5 <span class="token operator">=</span> <span class="token string">"string"</span><span class="token punctuation">;</span><span class="token comment">//常量池中的对象</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>str3 <span class="token operator">==</span> str4<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//false</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>str3 <span class="token operator">==</span> str5<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//true</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>str4 <span class="token operator">==</span> str5<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//false</span>

<span class="token class-name">String</span> a <span class="token operator">=</span> <span class="token string">"a"</span><span class="token punctuation">;</span>
<span class="token class-name">String</span> b <span class="token operator">=</span> a<span class="token punctuation">;</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">pringln</span><span class="token punctuation">(</span>a <span class="token operator">==</span> b<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token boolean">true</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
</li>
<li><p>String字符串有长度限制，在编译期，要求字符串常量池中的常量不能超过65535，并且在javac执行过程中控制了最大值为65534。<br>在运行期，长度不能超过Int的范围，否则会抛异常。</p>
</li>
<li><p>common-lang3包StringUtils</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java">isEmpty<span class="token comment">// 如果是null或者“”则返回true。</span>
isBlank<span class="token comment">// 如果是null或者“”或者空格或者制表符则返回true。isBlank判空更加准确。</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre></li>
</ul>
<table>
<thead>
<tr>
<th align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/ic5K1X6DQnBN37uv9cr-mg">java.lang.String的+号操作，这个谜终于要解开了！</a></th>
<th align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s?__biz=Mzg2MDYzODI5Nw==&mid=2247493885&idx=1&sn=024118e8044ce958c0602db7aba2f916&source=41#wechat_redirect">基础面试，为什么面试官总喜欢问String？</a></th>
<th align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/YvHt9uKKOdYZDdipGJXQYA">深入Java源码剖析之字符串常量</a></th>
</tr>
</thead>
<tbody><tr>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s?__biz=MzkzODE3OTI0Ng==&mid=2247491285&idx=1&sn=c9373feb23075bbada302740ec1a1434&source=41#wechat_redirect">浅谈Java中字符串的初始化及字符串操作类</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/xJnQq8voJvAyyyaBkrvc8A">5个刁钻的String面试题！</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/P-ijDf5IqtAwnFzDErQOyQ">为什么String是不可变的？</a></td>
</tr>
<tr>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/fwRcDog9_EU3nCEmDF_jcw">90%的同学都没搞清楚的Java字符串常量池问题（图文并茂）</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s?__biz=Mzg2MDYzODI5Nw==&mid=2247494306&idx=1&sn=9184273c5833a9a363b4ebb85163c1a5&source=41#wechat_redirect">Java中你以为的String其实并不完全正确</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/TuGw1tDVj53hIurRfA360g">再议String-字符串常量池与String.intern()</a></td>
</tr>
<tr>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/AWVJIvOAMERmw7lU1th4iQ">Java 9为何要将String的底层实现由char改成了byte?</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/iYO16IddRqAqn3D5VhURQQ">关于Java字符串的全部，都在这份手册里了</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/wR-HmIn8nO2UICVmJeqgGw">面试被问到了String相关的几道题，你能答上来吗？</a></td>
</tr>
</tbody></table>
<h3 id="try、catch、finally"><a href="#try、catch、finally" class="headerlink" title="try、catch、finally"></a>try、catch、finally</h3><ol>
<li>try中的return语句先执行了但并没有立即返回，等到finally执行结束后再return</li>
<li>finally块中的return语句会覆盖try块中的return返回</li>
<li>如果finally语句中没有return语句覆盖返回值，那么原来的返回值可能因为finally里的修改而改变也可能不变</li>
<li>try块里的return语句在异常的情况下不会被执行，这样具体返回哪个看情况</li>
<li>当发生异常后，catch中的return执行情况与未发生异常时try中return的执行情况完全一样</li>
<li>finally块的语句在try或catch中的return语句执行之后返回之前执行且finally里的修改语句可能影响也可能不影响try或catch中return已经确定的返回值，若finally里也有return语句则覆盖try或catch中的return语句直接返回。</li>
</ol>
<blockquote>
<p><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/0fdehvtTYz8Off52YOT8Qg">finally是在return之前还是之后执行</a><br><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/qYvMpbOXcJ77XQbK_MC8MA">面试官太难伺候？一个try-catch问出这么多花样</a></p>
</blockquote>
<h3 id="几个经典问题"><a href="#几个经典问题" class="headerlink" title="几个经典问题"></a>几个经典问题</h3><h4 id="equals和hashcode的关系"><a href="#equals和hashcode的关系" class="headerlink" title="equals和hashcode的关系"></a>equals和hashcode的关系</h4><ol>
<li>如果两个对象相同（即用equals比较返回true），那么它们的hashCode值一定要相同；</li>
<li>如果两个对象的hashCode相同，它们并不一定相同(即用equals比较返回false)</li>
</ol>
<h4 id="重写equals为什么需要重写hashcode"><a href="#重写equals为什么需要重写hashcode" class="headerlink" title="重写equals为什么需要重写hashcode"></a>重写equals为什么需要重写hashcode</h4><p>比较对象先比较hashcode，hashcode相同在比较equals。如果equals为true而hashcode不同的话就会造成hashmap的key可能会重复，因为jdk判断hashmap的key是否为重复首先判断hashcode是否一致，不一致的话直接判定不是同一个对象，而equals则判定对象为一个对象违背了hashmap的设计原则，重写hashCode方法，是为了在一些算法中避免我们不想要的冲突和碰撞</p>
<h4 id="Java为什么不支持多重继承"><a href="#Java为什么不支持多重继承" class="headerlink" title="Java为什么不支持多重继承"></a>Java为什么不支持多重继承</h4><p>有两个类B和C继承自A；假设B和C都继承了A的方法并且进行了覆盖，编写了自己的实现；假设D通过多重继承继承了B和C，那么D应该继承B和C的重载方法，那么它应该继承的是B的还是C的？这就陷入了矛盾，所以Java不允许多重继承。</p>
<h4 id="Java8中的接口和抽象类到底还有啥区别？"><a href="#Java8中的接口和抽象类到底还有啥区别？" class="headerlink" title="Java8中的接口和抽象类到底还有啥区别？"></a>Java8中的接口和抽象类到底还有啥区别？</h4><p>既然接口都能写默认方法了，那么还要抽象类干嘛呢？<br>区别1：首先抽象类是一个“类”，而接口只是一个“接口”，两者的概念和应用场景不一样，这也是抽象类和接口的主要区别。<br>区别2：即使在Java8中接口也能写实现方法了，但却不能写构造方法，而在抽象类是可以写构造方法的，意味着抽象类是参与类的实例化过程的，而接口则不是。抽象类不可以new<br>区别3：抽象类可以有自己的各种成员变量，并且可以通过自己的非抽象方法进行改变，而接口中的变量默认全是public static final修饰的，意味着都是常量，并且不能被自己和外部修改。<br>区别4：接口可以实现多继承，而抽象类只能单继承</p>
<h4 id="Comparable和Comparator区别"><a href="#Comparable和Comparator区别" class="headerlink" title="Comparable和Comparator区别"></a>Comparable和Comparator区别</h4><ul>
<li>一个类实现了Comparable接口，意味着该类的对象可以直接进行比较（排序），但比较（排序）的方式只有一种，很单一。一个类如果想要保持原样，又需要进行不同方式的比较（排序），就可以定制比较器（实现Comparator接口）。</li>
<li>Comparable接口在java.lang包下，而Comparator接口在java.util包下。</li>
<li>如果对象的排序需要基于自然顺序，请选择Comparable，如果需要按照对象的不同属性进行排序，请选择Comparator</li>
</ul>
<h4 id="过滤器和拦截器的区别"><a href="#过滤器和拦截器的区别" class="headerlink" title="过滤器和拦截器的区别"></a>过滤器和拦截器的区别</h4><ol>
<li>Filter需要在web.xml中配置，依赖于Servlet；Interceptor需要在SpringMVC中配置，依赖于框架；</li>
<li>Filter的执行顺序在Interceptor之前</li>
<li>两者的本质区别：拦截器（Interceptor）是基于Java的反射机制，而过滤器（Filter）是基于函数回调。从灵活性上说拦截器功能更强大些，Filter能做的事情，都能做，而且可以在请求前，请求后执行，比较灵活。Filter主要是针对URL地址做一个编码的事情、过滤掉没用的参数、安全校验（比较泛的，比如登录不登录之类），太细的话，还是建议用interceptor。不过还是根据不同情况选择合适的。</li>
<li>拦截器可以访问action上下文、值栈里的对象，而过滤器不能访问。</li>
<li>在action的生命周期中，拦截器可以多次被调用，而过滤器只能在容器初始化时被调用一次。init(),destroy()各一次，dofilter()多次</li>
<li>拦截的请求范围不同,过滤器几乎可以对所有进入容器的请求起作用，而拦截器只会对Controller中请求或访问static目录下的资源请求起作用。</li>
<li>触发时机不同，过滤器Filter是在请求进入容器后，但在进入servlet之前进行预处理，请求结束是在servlet处理完以后。拦截器Interceptor是在请求进入servlet后，在进入Controller之前进行预处理的，Controller中渲染了对应的视图之后请求结束。</li>
</ol>
<table>
<thead>
<tr>
<th align="center"><a target="_blank" rel="noopener" href="https://zhuanlan.zhihu.com/p/69060111?utm_source=wechat_timeline&amp;utm_medium=social&amp;utm_oi=1040923520439672832&amp;from=timeline">过滤器、监听器、拦截器的区别</a></th>
<th align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/PzQlCjLLM1fjPc0y4poQOw">一口气说出过滤器和拦截器6个区别</a></th>
<th align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/kZx10tnBJ8a6xtDgQsN1vQ">SpringBoot过滤器、拦截器、监听器对比及使用场景</a></th>
</tr>
</thead>
</table>
<h3 id="泛型"><a href="#泛型" class="headerlink" title="泛型"></a>泛型</h3><ul>
<li><p><code>List&lt;? extends Object &gt;</code>相当于<code>List&lt;?&gt;</code><br>在Java集合框架中，对于参数值是未知类型的容器类，只能读取其中元素，不能向其中添加元素,因为，其类型是未知，所以编译器无法识别添加元素的类型和容器的类型是否兼容，唯一的例外是NULL</p>
</li>
<li><p><code>List&lt;? extends T&gt;</code>可以添加元素添加的元素为T或T的子类</p>
</li>
<li><p><code>List&lt;? super T&gt;</code>可以添加元素添加的元素为T或T的父类</p>
</li>
</ul>
<blockquote>
<p><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/ilqFpf5kE0XzJnOv9SsX7Q">为了让你理解Java泛型，费了好大心思</a><br><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/5dWCpdyXNP5PeCZM8AQVAg">泛型中的T、E、K、V，还记得嘛？</a></p>
</blockquote>
<h3 id="CSRF和XSS的区别"><a href="#CSRF和XSS的区别" class="headerlink" title="CSRF和XSS的区别"></a>CSRF和XSS的区别</h3><ol>
<li>CSRF是跨站请求伪造;XSS是跨域脚本攻击。</li>
<li>CSRF需要用户先登录网站A,获取cookie;XSS不需要登录。</li>
<li>CSRF是利用网站A本身的漏洞,去请求网站A的api;XSS是向网站A注入JS代码,然后执行JS里的代码,篡改网站A的内容。（XSS利用的是站点内的信任用户，而CSRF则是通过伪装来自受信任用户的请求来利用受信任的网站。你可以这么理解CSRF攻击：攻击者盗用了你的身份，以你的名义向第三方网站发送恶意请求。）</li>
</ol>
<h3 id="web-xml详解"><a href="#web-xml详解" class="headerlink" title="web.xml详解"></a>web.xml详解</h3><p><strong>&lt;context-param&gt;</strong> 元素含有一对参数名和参数值，用作应用的Servlet上下文初始化参数，参数名在整个Web应用中必须是惟一的，在web应用的整个生命周期中上下文初始化参数都存在，任意的Servlet和jsp都可以随时随地访问它。在JSP网页中可以使用下列方法来取得：</p>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript">$<span class="token punctuation">&#123;</span>initParam<span class="token punctuation">.</span>param_name<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>
<p>若在Servlet可以使用下列方法来获得：</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">String</span> param_name<span class="token operator">=</span><span class="token function">getServletContext</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getInitParamter</span><span class="token punctuation">(</span><span class="token string">"param_name"</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>
<p>Servlet的ServletConfig对象拥有该Servlet的ServletContext的一个引用，所以可这样取得上下文初始化参数：</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token function">getServletConfig</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getServletContext</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getInitParameter</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>
<p>也可以在Servlet中直接调用</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token function">getServletContext</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getInitParameter</span><span class="token punctuation">(</span><span class="token punctuation">)</span>，<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>
<p>两者是等价的。</p>
<p><strong>&lt;listener&gt;</strong> 为web应用程序定义监听器，监听器用来监听各种事件，比如：application和session事件，所有的监听器按照相同的方式定义，功能取决去它们各自实现的接口，常用的Web事件接口有如下几个：</p>
<ol>
<li>ServletContextListener：用于监听Web应用的启动和关闭；</li>
<li>ServletContextAttributeListener：用于监听ServletContext范围（application）内属性的改变；</li>
<li>ServletRequestListener：用于监听用户的请求；</li>
<li>ServletRequestAttributeListener：用于监听ServletRequest范围（request）内属性的改变；</li>
<li>HttpSessionListener：用于监听用户session的开始和结束；</li>
<li>HttpSessionAttributeListener：用于监听HttpSession范围（session）内属性的改变</li>
</ol>
<p><strong>&lt;load-on-startup&gt;1&lt;&#x2F;load-on-startup&#x2F;&gt;</strong><br>load-on-startup元素标记容器是否应该在web应用程序启动的时候就加载这个servlet，(实例化并调用其init()方法)。它的值必须是一个整数，表示servlet被加载的先后顺序。如果该元素的值为负数或者没有设置，则容器会当Servlet被请求时再加载。如果值为正整数或者0时，表示容器在应用启动时就加载并初始化这个servlet，值越小，servlet的优先级越高，就越先被加载。值相同时，容器就会自己选择顺序来加载创建Servlet实例有两个时机较小的优先加载</p>
<h3 id="解决跨域的几种方案"><a href="#解决跨域的几种方案" class="headerlink" title="解决跨域的几种方案"></a>解决跨域的几种方案</h3><p>服务器没有同源策略，浏览器的同源策略是为了保证浏览器的安全性，实际上跨域请求会将请求发送到服务器，只不过在返回数据的时候浏览器发出跨域警告</p>
<ol>
<li>JSONP</li>
<li>CORS<br>服务器中编写过滤器允许跨域访问<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">HttpServletResponse</span> httpResponse <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token class-name">HttpServletResponse</span><span class="token punctuation">)</span> response<span class="token punctuation">;</span>
<span class="token comment">//该字段必填。它的值要么是请求时Origin字段的具体值，要么是一个*，表示接受任意域名的请求</span>
httpResponse<span class="token punctuation">.</span><span class="token function">setHeader</span><span class="token punctuation">(</span><span class="token string">"Access-Control-Allow-Origin"</span><span class="token punctuation">,</span> <span class="token string">"*"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">//该字段必填。它的值是逗号分隔的一个具体的字符串(GET,POST...)或者*，表明服务器支持的所有跨域请求的方法。注意，返回的是所有支持的方法，而不单是浏览器请求的那个方法。这是为了避免多次"预检"请求</span>
httpResponse<span class="token punctuation">.</span><span class="token function">setHeader</span><span class="token punctuation">(</span><span class="token string">"Access-Control-Allow-Methods"</span><span class="token punctuation">,</span> <span class="token string">"*"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">//该字段可选，用来指定本次预检请求的有效期，单位为秒。在有效期间，不用发出另一条预检请求。</span>
httpResponse<span class="token punctuation">.</span><span class="token function">setHeader</span><span class="token punctuation">(</span><span class="token string">"Access-Control-Max-Age"</span><span class="token punctuation">,</span> <span class="token string">"3600"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
httpResponse<span class="token punctuation">.</span><span class="token function">setHeader</span><span class="token punctuation">(</span><span class="token string">"Access-Control-Allow-Headers"</span><span class="token punctuation">,</span>
<span class="token string">"Origin,X-Requested-With, Content-Type, Accept, Connection, User-Agent, Cookie"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
httpResponse<span class="token punctuation">.</span><span class="token function">setHeader</span><span class="token punctuation">(</span><span class="token string">"Access-Control-Allow-Credentials"</span><span class="token punctuation">,</span> <span class="token string">"true"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
httpResponse<span class="token punctuation">.</span><span class="token function">setHeader</span><span class="token punctuation">(</span><span class="token string">"Content-type"</span><span class="token punctuation">,</span> <span class="token string">"application/json"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
httpResponse<span class="token punctuation">.</span><span class="token function">setHeader</span><span class="token punctuation">(</span><span class="token string">"Cache-Control"</span><span class="token punctuation">,</span> <span class="token string">"no-cache, must-revalidate"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
chain<span class="token punctuation">.</span><span class="token function">doFilter</span><span class="token punctuation">(</span>request<span class="token punctuation">,</span>httpResponse<span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></li>
<li>反向代理工具如nginx</li>
<li>服务器没有同源策略，使用HTTPClient等技术</li>
<li>控制器添加@CrossOrigin注解</li>
</ol>
<h2 id="gist"><a href="#gist" class="headerlink" title="gist"></a>gist</h2><h3 id="Java相关"><a href="#Java相关" class="headerlink" title="Java相关"></a>Java相关</h3><h4 id="fastjson转换"><a href="#fastjson转换" class="headerlink" title="fastjson转换"></a>fastjson转换</h4><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token comment">// json = &#123;"a":1,"b":2,"c":["d":3,"e":4]&#125;</span>
<span class="token comment">// json转JSONObject</span>
<span class="token class-name">JSONObject</span> a <span class="token operator">=</span> json<span class="token punctuation">.</span><span class="token function">getJSONObject</span><span class="token punctuation">(</span><span class="token string">"a"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// json转JSONArray</span>
<span class="token class-name">JSONArray</span> b <span class="token operator">=</span> json<span class="token punctuation">.</span><span class="token function">getJSONArray</span><span class="token punctuation">(</span><span class="token string">"c"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// json转对象</span>
<span class="token class-name">Map</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">,</span><span class="token class-name">Object</span><span class="token punctuation">></span></span> map <span class="token operator">=</span> <span class="token class-name">JSONObject</span><span class="token punctuation">.</span><span class="token function">parseObject</span><span class="token punctuation">(</span>json<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string">"c"</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token keyword">new</span> <span class="token class-name">TypeReference</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Map</span><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">,</span><span class="token class-name">Object</span><span class="token punctuation">></span><span class="token punctuation">></span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span><span class="token punctuation">&#125;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// 将Java对象序列化为JSON字符串，支持各种各种Java基本类型和JavaBean,避免value为null时过滤掉字段</span>
<span class="token class-name">JSONObject</span><span class="token punctuation">.</span><span class="token function">toJSONString</span><span class="token punctuation">(</span>data<span class="token punctuation">,</span><span class="token class-name">SerializerFeature<span class="token punctuation">.</span>WriteMapNullValue</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token constant">JSON</span><span class="token punctuation">.</span><span class="token function">parse</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token constant">JSON</span><span class="token punctuation">.</span><span class="token function">parseObject</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token constant">JSON</span><span class="token punctuation">.</span><span class="token function">parseArray</span><span class="token punctuation">(</span><span class="token punctuation">)</span>  json<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h4 id="获取路径"><a href="#获取路径" class="headerlink" title="获取路径"></a>获取路径</h4><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token comment">// http://localhost:8080/demo/course/index.jsp</span>
<span class="token comment">// 返回：/demo</span>
request<span class="token punctuation">.</span><span class="token function">getContextPath</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token comment">// 返回当前页面所在目录下全名称:/course/index.jsp</span>
request<span class="token punctuation">.</span><span class="token function">getServletPath</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token comment">// 返回浏览器地址栏地址 http://localhost:8080/demo/course/index.jsp</span>
request<span class="token punctuation">.</span><span class="token function">getRequestURL</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token comment">// 返回包含工程名的当前页面全路径：/demo/course/index.jsp</span>
request<span class="token punctuation">.</span><span class="token function">getRequestURI</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token comment">// 返回 http://localhost:8080</span>
request<span class="token punctuation">.</span><span class="token function">getScheme</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">+</span><span class="token string">"://"</span><span class="token operator">+</span>request<span class="token punctuation">.</span><span class="token function">getServerName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">+</span><span class="token string">":"</span><span class="token operator">+</span> request<span class="token punctuation">.</span><span class="token function">getServerPort</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token comment">// 返回E:\apache-tomcat-7.0.82\webapps\jnhouse\</span>
request<span class="token punctuation">.</span><span class="token function">getSession</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getServletContext</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getRealPath</span><span class="token punctuation">(</span><span class="token class-name">File</span><span class="token punctuation">.</span>separator<span class="token punctuation">)</span>
<span class="token comment">// 获取项目存放class文件的全路径 F:\zhongzhu\WebRoot\WEB-INF\classes</span>
<span class="token class-name">Thread</span><span class="token punctuation">.</span><span class="token function">currentThread</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getContextClassLoader</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getResource</span><span class="token punctuation">(</span><span class="token string">"/"</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getPath</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token comment">//从classpath路径下获取资源并返回一个InputStream供读取文件</span>
<span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">)</span>类名<span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">.</span><span class="token function">getClassLoader</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getResourceAsStream</span><span class="token punctuation">(</span><span class="token string">"name"</span><span class="token punctuation">)</span>
<span class="token comment">// 会在当前类所在的包结构下查找相应的资源</span>
类名<span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">.</span><span class="token function">getResourceAsStream</span><span class="token punctuation">(</span><span class="token string">"name"</span><span class="token punctuation">)</span>
<span class="token comment">// 请求转发</span>
<span class="token keyword">return</span> “forward<span class="token operator">:</span>forward2<span class="token punctuation">.</span>html”<span class="token punctuation">;</span>
<span class="token comment">// 重定向</span>
<span class="token keyword">return</span> “redirect<span class="token operator">:</span>redirect2<span class="token punctuation">.</span>html”<span class="token punctuation">;</span>
request<span class="token punctuation">.</span><span class="token function">getRequestDispatcher</span><span class="token punctuation">(</span><span class="token string">"url"</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">forward</span><span class="token punctuation">(</span>request<span class="token punctuation">,</span> response<span class="token punctuation">)</span>
response<span class="token punctuation">.</span><span class="token function">sendRedirect</span><span class="token punctuation">(</span><span class="token string">"leader.htm"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h4 id="list操作"><a href="#list操作" class="headerlink" title="list操作"></a>list操作</h4><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token comment">// 交集 listA内容变为listA和listB都存在的对象 listB不变</span>
listA<span class="token punctuation">.</span><span class="token function">retainAll</span><span class="token punctuation">(</span>listB<span class="token punctuation">)</span>
<span class="token comment">// 差集 listA中存在listB的内容去重 listB不变</span>
listA<span class="token punctuation">.</span><span class="token function">removeAll</span><span class="token punctuation">(</span>listB<span class="token punctuation">)</span>
<span class="token comment">// 并集,为了去重，listA先取差集，然后追加全部的listB listB不变</span>
listA<span class="token punctuation">.</span><span class="token function">removeAll</span><span class="token punctuation">(</span>listB<span class="token punctuation">)</span> listA<span class="token punctuation">.</span><span class="token function">addAll</span><span class="token punctuation">(</span>listB<span class="token punctuation">)</span>
<span class="token comment">// 将一个list平均切割2块</span>
<span class="token keyword">int</span> sublistSize <span class="token operator">=</span> <span class="token punctuation">(</span>list<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token operator">/</span> <span class="token number">2</span><span class="token punctuation">;</span>
<span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Integer</span><span class="token punctuation">></span></span> sublist1 <span class="token operator">=</span> list<span class="token punctuation">.</span><span class="token function">subList</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> sublistSize<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Integer</span><span class="token punctuation">></span></span> sublist2 <span class="token operator">=</span> list<span class="token punctuation">.</span><span class="token function">subList</span><span class="token punctuation">(</span>sublistSize<span class="token punctuation">,</span> originalList<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// 将切分后的两个子列表分别添加到新的ArrayList中</span>
resultList1<span class="token punctuation">.</span><span class="token function">addAll</span><span class="token punctuation">(</span>sublist1<span class="token punctuation">)</span><span class="token punctuation">;</span>
resultList2<span class="token punctuation">.</span><span class="token function">addAll</span><span class="token punctuation">(</span>sublist2<span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h4 id="深拷贝"><a href="#深拷贝" class="headerlink" title="深拷贝"></a>深拷贝</h4><ul>
<li>通过json化</li>
</ul>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">import</span> <span class="token import"><span class="token namespace">com<span class="token punctuation">.</span>fasterxml<span class="token punctuation">.</span>jackson<span class="token punctuation">.</span>databind<span class="token punctuation">.</span></span><span class="token class-name">ObjectMapper</span></span><span class="token punctuation">;</span>

<span class="token keyword">private</span> <span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">T</span><span class="token punctuation">></span></span> <span class="token class-name">T</span> <span class="token function">deepCopy</span><span class="token punctuation">(</span><span class="token class-name">T</span> source<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">JsonProcessingException</span><span class="token punctuation">&#123;</span>
    <span class="token class-name">ObjectMapper</span> objectMapper <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ObjectMapper</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token class-name">String</span> json <span class="token operator">=</span> objectMapper<span class="token punctuation">.</span><span class="token function">writeValueAsString</span><span class="token punctuation">(</span>source<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token class-name">T</span> deepCopyTarget <span class="token operator">=</span> objectMapper<span class="token punctuation">.</span><span class="token function">readValue</span><span class="token punctuation">(</span>json<span class="token punctuation">,</span> <span class="token keyword">new</span> <span class="token class-name">TypeReference</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">T</span><span class="token punctuation">></span></span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span><span class="token punctuation">&#125;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> deepCopyTarget<span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>
<span class="token comment">// --延申--</span>
<span class="token comment">//泛型方法,在方法名前指定类型参数（用尖括号&lt;>括起来）</span>
<span class="token keyword">public</span> <span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">T</span><span class="token punctuation">></span></span> <span class="token keyword">void</span> <span class="token function">methodName</span><span class="token punctuation">(</span><span class="token class-name">T</span> input<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>  
    <span class="token comment">// 方法体  </span>
<span class="token punctuation">&#125;</span>
<span class="token comment">//不加'&lt;>'的话T会被当做一个package里面的java类</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<ul>
<li>通过序列化</li>
</ul>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">import</span> <span class="token import"><span class="token namespace">java<span class="token punctuation">.</span>io<span class="token punctuation">.</span></span><span class="token operator">*</span></span><span class="token punctuation">;</span>  
  
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">DeepCopyViaSerialization</span> <span class="token keyword">implements</span> <span class="token class-name">Serializable</span> <span class="token punctuation">&#123;</span>  
    <span class="token comment">// 成员变量  </span>
  
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">T</span> <span class="token keyword">extends</span> <span class="token class-name">Serializable</span><span class="token punctuation">></span></span> <span class="token class-name">T</span> <span class="token function">deepCopy</span><span class="token punctuation">(</span><span class="token class-name">T</span> object<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>  
        <span class="token keyword">try</span> <span class="token punctuation">&#123;</span>  
            <span class="token comment">// 序列化到字节数组  </span>
            <span class="token class-name">ByteArrayOutputStream</span> bos <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ByteArrayOutputStream</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>  
            <span class="token class-name">ObjectOutputStream</span> out <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ObjectOutputStream</span><span class="token punctuation">(</span>bos<span class="token punctuation">)</span><span class="token punctuation">;</span>  
            out<span class="token punctuation">.</span><span class="token function">writeObject</span><span class="token punctuation">(</span>object<span class="token punctuation">)</span><span class="token punctuation">;</span>  
  
            <span class="token comment">// 反序列化到新对象  </span>
            <span class="token class-name">ByteArrayInputStream</span> bis <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ByteArrayInputStream</span><span class="token punctuation">(</span>bos<span class="token punctuation">.</span><span class="token function">toByteArray</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>  
            <span class="token class-name">ObjectInputStream</span> in <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ObjectInputStream</span><span class="token punctuation">(</span>bis<span class="token punctuation">)</span><span class="token punctuation">;</span>  
  
            <span class="token annotation punctuation">@SuppressWarnings</span><span class="token punctuation">(</span><span class="token string">"unchecked"</span><span class="token punctuation">)</span>  
            <span class="token class-name">T</span> copiedObj <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token class-name">T</span><span class="token punctuation">)</span> in<span class="token punctuation">.</span><span class="token function">readObject</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>  
            <span class="token keyword">return</span> copiedObj<span class="token punctuation">;</span>  
        <span class="token punctuation">&#125;</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">IOException</span> <span class="token operator">|</span> <span class="token class-name">ClassNotFoundException</span> e<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>  
            <span class="token keyword">throw</span> <span class="token keyword">new</span> <span class="token class-name">RuntimeException</span><span class="token punctuation">(</span>e<span class="token punctuation">)</span><span class="token punctuation">;</span>  
        <span class="token punctuation">&#125;</span>  
    <span class="token punctuation">&#125;</span>  
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<ul>
<li>使用三方库<br>一些第三方库如Apache Commons Lang的SerializationUtils也提供了序列化和反序列化的深拷贝方法，使用这些库可以简化深拷贝的实现。<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>apache<span class="token punctuation">.</span>commons<span class="token punctuation">.</span>lang3<span class="token punctuation">.</span></span><span class="token class-name">SerializationUtils</span></span><span class="token punctuation">;</span>  
  
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">DeepCopyWithApacheCommons</span> <span class="token punctuation">&#123;</span>  
    <span class="token comment">// 假设有一个类  </span>
  
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">T</span> <span class="token keyword">extends</span> <span class="token class-name">Serializable</span><span class="token punctuation">></span></span> <span class="token class-name">T</span> <span class="token function">deepCopy</span><span class="token punctuation">(</span><span class="token class-name">T</span> object<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>  
        <span class="token keyword">return</span> <span class="token class-name">SerializationUtils</span><span class="token punctuation">.</span><span class="token function">clone</span><span class="token punctuation">(</span>object<span class="token punctuation">)</span><span class="token punctuation">;</span>  
    <span class="token punctuation">&#125;</span>  
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></li>
</ul>
<h4 id="代码分页"><a href="#代码分页" class="headerlink" title="代码分页"></a>代码分页</h4><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">Integer</span> page <span class="token operator">=</span> map<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string">"page"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">Integer</span> limit <span class="token operator">=</span> map<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string">"rows"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">Integer</span> start <span class="token operator">=</span> <span class="token punctuation">(</span>page<span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token operator">*</span>limit<span class="token punctuation">;</span>
<span class="token comment">//List&lt;Map&lt;String,Object>> subList = list.subList(start,start+limit>list.size()?list.size():start+limit)</span>
<span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Map</span><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">,</span><span class="token class-name">Object</span><span class="token punctuation">></span><span class="token punctuation">></span></span> subList <span class="token operator">=</span> list<span class="token punctuation">.</span><span class="token function">subList</span><span class="token punctuation">(</span>start<span class="token punctuation">,</span><span class="token class-name">Math</span><span class="token punctuation">.</span><span class="token function">min</span><span class="token punctuation">(</span>start<span class="token operator">+</span>limit<span class="token punctuation">,</span>list<span class="token punctuation">.</span>size<span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token comment">// stream</span>
 <span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">></span></span> pageData <span class="token operator">=</span> list<span class="token punctuation">.</span><span class="token function">stream</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
            <span class="token punctuation">.</span><span class="token function">skip</span><span class="token punctuation">(</span><span class="token punctuation">(</span>pageNum <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token operator">*</span> pageSize<span class="token punctuation">)</span> <span class="token comment">// 跳过前面的数据</span>
            <span class="token punctuation">.</span><span class="token function">limit</span><span class="token punctuation">(</span>pageSize<span class="token punctuation">)</span>                 <span class="token comment">// 限制每页大小</span>
            <span class="token punctuation">.</span><span class="token function">collect</span><span class="token punctuation">(</span><span class="token class-name">Collectors</span><span class="token punctuation">.</span><span class="token function">toList</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>   <span class="token comment">// 收集结果</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h4 id="正则表达式"><a href="#正则表达式" class="headerlink" title="正则表达式"></a>正则表达式</h4><p>匹配数字，包括小数</p>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token keyword">var</span> patten <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">^[+-]?(0|([1-9]\d*))(\.\d+)?$</span><span class="token regex-delimiter">/</span></span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>
<p>[+-] 中括号表示其内的内容都是符合要求的匹配，所以这个表示“+”或”-“<br>Matcher 类中group(0) 表示正则表达式中符合条件的字符串。<br>Matcher 类中group(1) 表示正则表达式中符合条件的字符串中的第一个()中的字符串。<br>Matcher 类中group(2) 表示正则表达式中符合条件的字符串中的第二个()中的字符串。<br>Matcher 类中group(3) 表示正则表达式中符合条件的字符串中的第三个()中的字符串。<br>group是针对()来说的，group(0)就是指的整个串，group(1)指的是第一个括号里的东西，group(2)指的第二个括号里的东西。</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">String</span> line <span class="token operator">=</span> <span class="token string">"123ra9040 123123aj234 adf12322ad 222jsk22"</span><span class="token punctuation">;</span>
<span class="token class-name">String</span> pattern <span class="token operator">=</span> <span class="token string">"(\\d+)([a-z]+)(\\d+)"</span><span class="token punctuation">;</span>
<span class="token comment">// 创建Pattern对象</span>
<span class="token class-name">Pattern</span> r <span class="token operator">=</span> <span class="token class-name">Pattern</span><span class="token punctuation">.</span><span class="token function">compile</span><span class="token punctuation">(</span>pattern<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// 现在创建matcher对象</span>
<span class="token class-name">Matcher</span> m <span class="token operator">=</span> r<span class="token punctuation">.</span><span class="token function">matcher</span><span class="token punctuation">(</span>line<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// m.find是否找到正则表达式中符合条件的字符串</span>
<span class="token keyword">while</span> <span class="token punctuation">(</span>m<span class="token punctuation">.</span><span class="token function">find</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
    <span class="token comment">// 拿到上面匹配到的数据</span>
    <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"Found value: "</span> <span class="token operator">+</span> m<span class="token punctuation">.</span><span class="token function">group</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"Found value: "</span> <span class="token operator">+</span> m<span class="token punctuation">.</span><span class="token function">group</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"Found value: "</span> <span class="token operator">+</span> m<span class="token punctuation">.</span><span class="token function">group</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span> <span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"Found value: "</span> <span class="token operator">+</span> m<span class="token punctuation">.</span><span class="token function">group</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span> <span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p>group(0)对应着((&#x2F;&#x2F;d+)([a-z]+)(&#x2F;&#x2F;d+))所匹配的数据123ra9040或者123123aj234或者222jsk22<br>group(2)输出的数据是group(0)中所匹配的数据,第二个括号的表达式,也就是([a-z]+)匹配到是数据ra或者aj或者jsk<br>group(3)输出的数据是group(0)中所匹配的数据,第三个括号的表达式,也就是(&#x2F;&#x2F;d+)匹配到是数据9040或者234或者22</p>
<p>JS使用正则表达式</p>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token comment">// 可以和java正则表达式共用，如果不使用new RegExp()，则和java正则表达式书写方式有差异</span>
<span class="token keyword">var</span> patten <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">RegExp</span><span class="token punctuation">(</span><span class="token string">"..."</span><span class="token punctuation">)</span>
patten<span class="token punctuation">.</span><span class="token function">text</span><span class="token punctuation">(</span>要匹配的内容<span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>

<table>
<thead>
<tr>
<th align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/40UCh57rBu9LafTfjEQh1g">对正则表达式，这么多年你还在害怕吗</a></th>
<th align="center"><a target="_blank" rel="noopener" href="https://tool.oschina.net/uploads/apidocs/jquery/regexp.html">正则表达式手册</a></th>
<th align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/ABCqcJPNf1VJ7ByGrVlS_A">常用正则表达式最强整理（速查手册）</a></th>
</tr>
</thead>
<tbody><tr>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/j5rAEyu1k4edpk5T8NuGFw">给懒人开发者的一份正则表达式指南</a></td>
<td align="center"></td>
<td align="center"></td>
</tr>
</tbody></table>
<h4 id="冒泡排序"><a href="#冒泡排序" class="headerlink" title="冒泡排序"></a>冒泡排序</h4><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> arr <span class="token operator">=</span> <span class="token punctuation">&#123;</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">&#125;</span><span class="token punctuation">;</span>
<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>i<span class="token operator">&lt;</span>arr<span class="token punctuation">.</span>length<span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
　　<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> j<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>j<span class="token operator">&lt;</span>arr<span class="token punctuation">.</span>length<span class="token operator">-</span><span class="token number">1</span><span class="token operator">-</span>i<span class="token punctuation">;</span>j<span class="token operator">++</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
　　　　<span class="token keyword">if</span><span class="token punctuation">(</span>arr<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token operator">></span>arr<span class="token punctuation">[</span>j<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
　　　　　　<span class="token keyword">int</span> temp<span class="token operator">=</span>arr<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">;</span>
　　　　　　arr<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token operator">=</span>arr<span class="token punctuation">[</span>j<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
　　　　　　arr<span class="token punctuation">[</span>j<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token operator">=</span>temp<span class="token punctuation">;</span>
　　　　<span class="token punctuation">&#125;</span>
　　<span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span> <span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h4 id="JDK8-base64转换"><a href="#JDK8-base64转换" class="headerlink" title="JDK8 base64转换"></a>JDK8 base64转换</h4><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">String</span> str <span class="token operator">=</span> <span class="token string">"str"</span><span class="token punctuation">;</span>
<span class="token class-name">String</span> encoded <span class="token operator">=</span> <span class="token class-name">Base64</span><span class="token punctuation">.</span><span class="token function">getEncoder</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">encodeToString</span><span class="token punctuation">(</span>str<span class="token punctuation">.</span><span class="token function">getBytes</span><span class="token punctuation">(</span> <span class="token class-name">StandardCharsets</span><span class="token punctuation">.</span><span class="token constant">UTF_8</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">String</span> decoded <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">String</span><span class="token punctuation">(</span><span class="token class-name">Base64</span><span class="token punctuation">.</span><span class="token function">getDecoder</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">decode</span><span class="token punctuation">(</span>encoded<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token class-name">StandardCharsets</span><span class="token punctuation">.</span><span class="token constant">UTF_8</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>

<h4 id="poi-excel"><a href="#poi-excel" class="headerlink" title="poi excel"></a>poi excel</h4><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">creatExcel</span><span class="token punctuation">(</span><span class="token class-name">File</span> file<span class="token punctuation">,</span><span class="token class-name">String</span> code<span class="token punctuation">,</span><span class="token class-name">String</span> ts<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
	<span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> header<span class="token operator">=</span><span class="token punctuation">&#123;</span><span class="token string">"有功功率"</span><span class="token punctuation">,</span><span class="token string">"时间"</span><span class="token punctuation">,</span><span class="token string">"pss输出信号"</span><span class="token punctuation">,</span><span class="token string">"时间"</span><span class="token punctuation">&#125;</span><span class="token punctuation">;</span>
	<span class="token class-name">Workbook</span> wb <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">XSSFWorkbook</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token class-name">Sheet</span> sheet <span class="token operator">=</span> wb<span class="token punctuation">.</span><span class="token function">createSheet</span><span class="token punctuation">(</span><span class="token string">"sheet1"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token class-name">Row</span> rowFirst <span class="token operator">=</span> sheet<span class="token punctuation">.</span><span class="token function">createRow</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>i<span class="token operator">&lt;</span>header<span class="token punctuation">.</span>length<span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
		sheet<span class="token punctuation">.</span><span class="token function">setColumnWidth</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> <span class="token number">5000</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
	<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>i<span class="token operator">&lt;</span>header<span class="token punctuation">.</span>length<span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
		<span class="token class-name">Cell</span> cell <span class="token operator">=</span> rowFirst<span class="token punctuation">.</span><span class="token function">createCell</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">;</span>
		cell<span class="token punctuation">.</span><span class="token function">setCellValue</span><span class="token punctuation">(</span>header<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
	<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>i<span class="token operator">&lt;</span>maxSize<span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
		<span class="token class-name">Row</span> row <span class="token operator">=</span> sheet<span class="token punctuation">.</span><span class="token function">createRow</span><span class="token punctuation">(</span>i<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		row<span class="token punctuation">.</span><span class="token function">createCell</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">setCellValue</span><span class="token punctuation">(</span><span class="token string">""</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		row<span class="token punctuation">.</span><span class="token function">createCell</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">setCellValue</span><span class="token punctuation">(</span><span class="token string">""</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		row<span class="token punctuation">.</span><span class="token function">createCell</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">setCellValue</span><span class="token punctuation">(</span><span class="token string">""</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		row<span class="token punctuation">.</span><span class="token function">createCell</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">setCellValue</span><span class="token punctuation">(</span><span class="token string">""</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
    <span class="token keyword">try</span><span class="token punctuation">&#123;</span><span class="token comment">// 指定本地文件流</span>
	    <span class="token class-name">OutputStream</span> os <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">FileOutputStream</span><span class="token punctuation">(</span>file<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">// excel写入</span>
        wb<span class="token punctuation">.</span><span class="token function">write</span><span class="token punctuation">(</span>os<span class="token punctuation">)</span><span class="token punctuation">;</span>
        os<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span><span class="token keyword">catch</span><span class="token punctuation">(</span><span class="token class-name">Exception</span> e<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
		e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>

<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">excel</span><span class="token punctuation">(</span><span class="token class-name">HttpServletRequest</span> request<span class="token punctuation">,</span><span class="token class-name">HttpServletResponse</span> response<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
	<span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> handers <span class="token operator">=</span> <span class="token punctuation">&#123;</span><span class="token string">"id"</span><span class="token punctuation">,</span><span class="token string">"书名"</span><span class="token punctuation">,</span><span class="token string">"作者"</span><span class="token punctuation">,</span><span class="token string">"价格"</span><span class="token punctuation">&#125;</span><span class="token punctuation">;</span>
	<span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Book</span><span class="token punctuation">></span></span> list <span class="token operator">=</span> masterMapper<span class="token punctuation">.</span><span class="token function">querySome</span><span class="token punctuation">(</span><span class="token keyword">null</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">try</span><span class="token punctuation">&#123;</span>
		<span class="token class-name">String</span> filedisplay <span class="token operator">=</span> <span class="token string">"test.xlsx"</span><span class="token punctuation">;</span>
		filedisplay <span class="token operator">=</span> <span class="token class-name">URLEncoder</span><span class="token punctuation">.</span><span class="token function">encode</span><span class="token punctuation">(</span>filedisplay<span class="token punctuation">,</span> <span class="token string">"UTF-8"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>			
		<span class="token comment">//由浏览器指定下载路径</span>
		<span class="token comment">//response.reset();			</span>
		<span class="token comment">//response.setContentType("application/x-download");</span>
		<span class="token comment">//response.setContentType("application/vnd.ms-excel;charset=utf-8");</span>
		response<span class="token punctuation">.</span><span class="token function">addHeader</span><span class="token punctuation">(</span><span class="token string">"Content-Disposition"</span><span class="token punctuation">,</span> <span class="token string">"attachment;filename="</span><span class="token operator">+</span> filedisplay<span class="token punctuation">)</span><span class="token punctuation">;</span>
		request<span class="token punctuation">.</span><span class="token function">setCharacterEncoding</span><span class="token punctuation">(</span><span class="token string">"UTF-8"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		response<span class="token punctuation">.</span><span class="token function">setContentType</span><span class="token punctuation">(</span><span class="token string">"APPLICATION/OCTET-STREAM"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		response<span class="token punctuation">.</span><span class="token function">setHeader</span><span class="token punctuation">(</span><span class="token string">"Content-Dispostion"</span><span class="token punctuation">,</span><span class="token string">"attachment;filename="</span><span class="token punctuation">.</span><span class="token function">concat</span><span class="token punctuation">(</span>filedisplay<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
			
		<span class="token class-name">HSSFWorkbook</span> wb <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">HSSFWorkbook</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//创建工作簿</span>
		<span class="token class-name">HSSFSheet</span> sheet <span class="token operator">=</span> wb<span class="token punctuation">.</span><span class="token function">createSheet</span><span class="token punctuation">(</span><span class="token string">"操作"</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//第一个sheet</span>
		<span class="token class-name">HSSFRow</span> rowFirst <span class="token operator">=</span> sheet<span class="token punctuation">.</span><span class="token function">createRow</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//第一个sheet第一行为标题</span>
		rowFirst<span class="token punctuation">.</span><span class="token function">setHeight</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token keyword">short</span><span class="token punctuation">)</span> <span class="token number">500</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">HSSFCellStyle</span> cellStyle <span class="token operator">=</span> wb<span class="token punctuation">.</span><span class="token function">createCellStyle</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">// 创建单元格样式对象</span>
        cellStyle<span class="token punctuation">.</span><span class="token function">setAlignment</span><span class="token punctuation">(</span><span class="token class-name">HorizontalAlignment</span><span class="token punctuation">.</span><span class="token constant">CENTER</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 居中</span>
        cellStyle<span class="token punctuation">.</span><span class="token function">setVerticalAlignment</span><span class="token punctuation">(</span><span class="token class-name">VerticalAlignment</span><span class="token punctuation">.</span><span class="token constant">CENTER</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> handers<span class="token punctuation">.</span>length<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
		  sheet<span class="token punctuation">.</span><span class="token function">setColumnWidth</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> <span class="token number">4000</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">// 设置列宽</span>
		<span class="token punctuation">&#125;</span>
		<span class="token comment">//写标题了</span>
		<span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> handers<span class="token punctuation">.</span>length<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
			<span class="token comment">//获取第一行的每一个单元格</span>
			<span class="token class-name">HSSFCell</span> cell <span class="token operator">=</span> rowFirst<span class="token punctuation">.</span><span class="token function">createCell</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">;</span>
			<span class="token comment">//往单元格里面写入值</span>
			cell<span class="token punctuation">.</span><span class="token function">setCellValue</span><span class="token punctuation">(</span>handers<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
			cell<span class="token punctuation">.</span><span class="token function">setCellStyle</span><span class="token punctuation">(</span>cellStyle<span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token punctuation">&#125;</span>
		<span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> list<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
			<span class="token class-name">Book</span> u <span class="token operator">=</span> list<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">;</span>			
			<span class="token comment">//创建数据行</span>
			<span class="token class-name">HSSFRow</span> row <span class="token operator">=</span> sheet<span class="token punctuation">.</span><span class="token function">createRow</span><span class="token punctuation">(</span>i <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>				
			row<span class="token punctuation">.</span><span class="token function">setHeight</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token keyword">short</span><span class="token punctuation">)</span> <span class="token number">400</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 设置每行的高度</span>
			<span class="token comment">//设置对应单元格的值</span>
	        row<span class="token punctuation">.</span><span class="token function">createCell</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">setCellValue</span><span class="token punctuation">(</span>u<span class="token punctuation">.</span><span class="token function">getId</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            row<span class="token punctuation">.</span><span class="token function">getCell</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">setCellStyle</span><span class="token punctuation">(</span>cellStyle<span class="token punctuation">)</span><span class="token punctuation">;</span>
            row<span class="token punctuation">.</span><span class="token function">createCell</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">setCellValue</span><span class="token punctuation">(</span>u<span class="token punctuation">.</span><span class="token function">getBookname</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            row<span class="token punctuation">.</span><span class="token function">getCell</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">setCellStyle</span><span class="token punctuation">(</span>cellStyle<span class="token punctuation">)</span><span class="token punctuation">;</span>
            row<span class="token punctuation">.</span><span class="token function">createCell</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">setCellValue</span><span class="token punctuation">(</span>u<span class="token punctuation">.</span><span class="token function">getBookauthor</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            row<span class="token punctuation">.</span><span class="token function">getCell</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">setCellStyle</span><span class="token punctuation">(</span>cellStyle<span class="token punctuation">)</span><span class="token punctuation">;</span>
            row<span class="token punctuation">.</span><span class="token function">createCell</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">setCellValue</span><span class="token punctuation">(</span>u<span class="token punctuation">.</span><span class="token function">getBookprice</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            row<span class="token punctuation">.</span><span class="token function">getCell</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">setCellStyle</span><span class="token punctuation">(</span>cellStyle<span class="token punctuation">)</span><span class="token punctuation">;</span>

	<span class="token punctuation">&#125;</span>
        <span class="token class-name">OutputStream</span> os <span class="token operator">=</span> response<span class="token punctuation">.</span><span class="token function">getOutputStream</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        wb<span class="token punctuation">.</span><span class="token function">write</span><span class="token punctuation">(</span>os<span class="token punctuation">)</span><span class="token punctuation">;</span>
        os<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        wb<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span><span class="token keyword">catch</span><span class="token punctuation">(</span><span class="token class-name">Exception</span> e<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
		e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h4 id="ServletRequest常用方法"><a href="#ServletRequest常用方法" class="headerlink" title="ServletRequest常用方法"></a>ServletRequest常用方法</h4><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">Object</span> <span class="token function">getAttribute</span><span class="token punctuation">(</span><span class="token class-name">String</span> name<span class="token punctuation">)</span>	
<span class="token comment">// 以Object形式返回指定属性的值，如果不存在给定名称的属性，则返回null。</span>
<span class="token class-name">Enumeration</span>	<span class="token function">getAttributeNames</span><span class="token punctuation">(</span><span class="token punctuation">)</span>	
<span class="token comment">// 返回包含此请求可用属性的名称的Enumeration。如果该请求没有可用的属性，则此方法返回一个空的Enumeration。</span>
<span class="token class-name">String</span> <span class="token function">getCharacterEncoding</span><span class="token punctuation">(</span><span class="token punctuation">)</span>	
<span class="token comment">// 返回此请求正文中使用的字符编码的名称。如果该请求未指定字符编码，则此方法返回null</span>
<span class="token keyword">void</span> <span class="token function">setCharacterEncoding</span><span class="token punctuation">(</span><span class="token class-name">String</span> env<span class="token punctuation">)</span>	
<span class="token comment">// 重写此请求正文中使用的字符编码的名称。必须在使用getReader() 读取请求参数或读取输入之前调用此方法。否则，此方法没有任何效果。</span>
<span class="token keyword">int</span> <span class="token function">getContentLength</span><span class="token punctuation">(</span><span class="token punctuation">)</span>	
<span class="token comment">// 返回请求正文的长度（以字节为单位），并使输入流可以使用它，如果长度未知，则返回-1。对于HTTP servlet，返回的值与CGI变量 CONTENT_LENGTH的值相同。</span>
<span class="token class-name">String</span> <span class="token function">getContentType</span><span class="token punctuation">(</span><span class="token punctuation">)</span>	
<span class="token comment">// 返回请求正文的MIME类型，如果该类型未知，则返回null。对于HTTP servlet，返回的值与CGI变量CONTENT_TYPE的值相同。</span>
<span class="token class-name">ServletInputStream</span> <span class="token function">getInputStream</span><span class="token punctuation">(</span><span class="token punctuation">)</span>	
<span class="token comment">// 使用ServletInputStream以二进制数据形式获取请求正文。可调用此方法或getReader读取正文，而不是两种方法都调用。</span>
<span class="token class-name">String</span> <span class="token function">getParameter</span><span class="token punctuation">(</span><span class="token class-name">String</span> name<span class="token punctuation">)</span>	
<span class="token comment">// 以String形式返回请求参数的值，如果该参数不存在，则返回null。请求参数是与请求一起发送的额外信息。对于HTTP servlet，参数包含在查询字符串或发送的表单数据中。</span>
<span class="token class-name">Enumeration</span>	<span class="token function">getParameterNames</span><span class="token punctuation">(</span><span class="token punctuation">)</span>	
<span class="token comment">// 返回包含此请求中所包含参数的名称的String对象的Enumeration。如果该请求没有参数，则此方法返回一个空的Enumeration。</span>
<span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token function">getParameterValues</span><span class="token punctuation">(</span><span class="token class-name">String</span> name<span class="token punctuation">)</span>	
<span class="token comment">// 返回包含给定请求参数拥有的所有值的String对象数组，如果该参数不存在，则返回null。</span>
<span class="token class-name">Map</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">K</span><span class="token punctuation">,</span> <span class="token class-name">V</span><span class="token punctuation">></span></span> <span class="token function">getParameterMap</span><span class="token punctuation">(</span><span class="token punctuation">)</span>	
<span class="token comment">// 返回此请求的参数的 java.util.Map。请求参数是与请求一起发送的额外信息。对于HTTP servlet，参数包含在查询字符串或发送的表单数据中。</span>
<span class="token class-name">String</span> <span class="token function">getProtocol</span><span class="token punctuation">(</span><span class="token punctuation">)</span>	
<span class="token comment">// 以protocol/majorVersion.minorVersion的形式（例如HTTP/1.1）返回请求使用的协议的名称和版本。对于HTTP servlet，返回的值与CGI变量SERVER_PROTOCOL的值相同。</span>
<span class="token class-name">String</span> <span class="token function">getScheme</span><span class="token punctuation">(</span><span class="token punctuation">)</span>	
<span class="token comment">// 返回用于发出此请求的方案的名称，例如http、https或ftp。不同方案具有不同的构造URL的规则，这一点已在RFC 1738中注明。</span>
<span class="token class-name">String</span> <span class="token function">getServerName</span><span class="token punctuation">(</span><span class="token punctuation">)</span>	
<span class="token comment">// 返回请求被发送到的服务器的主机名。它是Host头值“:”（如果有）之前的那部分的值，或者解析的服务器名称或服务器IP地址。</span>
<span class="token keyword">int</span>	<span class="token function">getServerPort</span><span class="token punctuation">(</span><span class="token punctuation">)</span>	<span class="token comment">// 返回请求被发送到的端口号。它是Host头值“:”（如果有）之后的那部分的值，或者接受客户端连接的服务器端口。</span>
<span class="token class-name">BufferedReader</span> <span class="token function">getReader</span><span class="token punctuation">(</span><span class="token punctuation">)</span>	
<span class="token comment">// 使用BufferedReader以字符数据形式获取请求正文。读取器根据正文上使用的字符编码转换字符数据。可调用此方法或getInputStream读取正文，而不是两种方法都调用。</span>
<span class="token class-name">String</span> <span class="token function">getRemoteAddr</span><span class="token punctuation">(</span><span class="token punctuation">)</span>	
<span class="token comment">// 返回发送请求的客户端或最后一个代理的Internet Protocol (IP)地址。对于HTTP servlet，返回的值与CGI变量REMOTE_ADDR的值相同。</span>
<span class="token class-name">String</span> <span class="token function">getRemoteHost</span><span class="token punctuation">(</span><span class="token punctuation">)</span>	
<span class="token comment">// 返回发送请求的客户端或最后一个代理的完全限定名称。如果引擎无法或没有选择解析主机名（为了提高性能），则此方法返回以点分隔的字符串形式的IP地址。对于HTTP servlet，返回的值与CGI变量REMOTE_HOST的值相同。</span>
<span class="token keyword">void</span> <span class="token function">setAttribute</span><span class="token punctuation">(</span><span class="token class-name">String</span> name<span class="token punctuation">,</span> <span class="token class-name">Object</span> o<span class="token punctuation">)</span>	
<span class="token comment">// 存储此请求中的属性。在请求之间重置属性。此方法常常与RequestDispatcher一起使用。</span>
<span class="token keyword">void</span> <span class="token function">removeAttribute</span><span class="token punctuation">(</span><span class="token class-name">String</span> name<span class="token punctuation">)</span>	
<span class="token comment">// 从此请求中移除属性。此方法不是普遍需要的，因为属性只在处理请求期间保留。</span>
<span class="token class-name">Locale</span> <span class="token function">getLocale</span><span class="token punctuation">(</span><span class="token punctuation">)</span>	
<span class="token comment">// 基于Accept-Language头，返回客户端将用来接受内容的首选Locale。如果客户端请求没有提供Accept-Language头，则此方法返回服务器的默认语言环境。</span>
<span class="token class-name">Enumeration</span>	<span class="token function">getLocales</span><span class="token punctuation">(</span><span class="token punctuation">)</span>	
<span class="token comment">// 返回Locale对象的Enumeration，这些对象以首选语言环境开头，按递减顺序排列，指示基于Accept-Language头客户端可接受的语言环境。如果客户端请求没有提供Accept-Language头，则此方法返回包含一个Locale的Enumeration，即服务器的默认语言环境。</span>
<span class="token keyword">boolean</span>	<span class="token function">isSecure</span><span class="token punctuation">(</span><span class="token punctuation">)</span>	
<span class="token comment">// 返回一个boolean值，指示此请求是否是使用安全通道（比如HTTPS）发出的。</span>
<span class="token class-name">RequestDispatcher</span> <span class="token function">getRequestDispatcher</span><span class="token punctuation">(</span><span class="token class-name">String</span> path<span class="token punctuation">)</span>	
<span class="token comment">// 返回一个RequestDispatcher对象，它充当位于给定路径上的资源的包装器。可以使用RequestDispatcher对象将请求转发给资源，或者在响应中包含资源。资源可以是动态的，也可以是静态的。</span>
<span class="token class-name">String</span> <span class="token function">getRealPath</span><span class="token punctuation">(</span><span class="token class-name">String</span> path<span class="token punctuation">)</span>	
<span class="token comment">// 从Java Servlet API的版本2.1起，请改用ServletContext#getRealPath</span>
<span class="token keyword">int</span> <span class="token function">getRemotePort</span><span class="token punctuation">(</span><span class="token punctuation">)</span>	
<span class="token comment">// 返回发送请求的客户端或最后一个代理的Internet Protocol (IP)源端口。</span>
<span class="token class-name">String</span> <span class="token function">getLocalName</span><span class="token punctuation">(</span><span class="token punctuation">)</span>	
<span class="token comment">// 返回接收请求的Internet Protocol (IP)接口的主机名。</span>
<span class="token class-name">String</span> <span class="token function">getLocalAddr</span><span class="token punctuation">(</span><span class="token punctuation">)</span>	
<span class="token comment">// 返回接收请求的接口的Internet Protocol (IP)地址。</span>
<span class="token keyword">int</span> <span class="token function">getLocalPort</span><span class="token punctuation">(</span><span class="token punctuation">)</span>	
<span class="token comment">// 返回接收请求的接口的Internet Protocol (IP)端口号。</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h4 id="HttpServletRequest"><a href="#HttpServletRequest" class="headerlink" title="HttpServletRequest"></a>HttpServletRequest</h4><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">Cookies</span>	<span class="token function">getCookies</span><span class="token punctuation">(</span><span class="token punctuation">)</span>	
<span class="token comment">// 返回包含客户端随此请求一起发送的所有Cookie对象的数组。</span>
<span class="token keyword">long</span> <span class="token function">getDateHeader</span><span class="token punctuation">(</span><span class="token class-name">String</span> name<span class="token punctuation">)</span>	
<span class="token comment">// 以表示Date对象的long值的形式返回指定请求头的值。</span>
<span class="token class-name">String</span> <span class="token function">getHeader</span><span class="token punctuation">(</span><span class="token class-name">String</span> name<span class="token punctuation">)</span>	
<span class="token comment">// 以String的形式返回指定请求头的值。</span>
<span class="token class-name">Enumeration</span>	<span class="token function">getHeaders</span><span class="token punctuation">(</span><span class="token class-name">String</span> name<span class="token punctuation">)</span>	
<span class="token comment">// 以String对象的Enumeration的形式返回指定请求头的所有值。</span>
<span class="token class-name">Enumeration</span>	<span class="token function">getHeaderNames</span><span class="token punctuation">(</span><span class="token punctuation">)</span>	
<span class="token comment">// 返回此请求包含的所有头名称的枚举。如果该请求没有头，则此方法返回一个空枚举。</span>
<span class="token keyword">int</span> <span class="token function">getIntHeader</span><span class="token punctuation">(</span><span class="token class-name">String</span> name<span class="token punctuation">)</span>	
<span class="token comment">// 以int的形式返回指定请求头的值。</span>
<span class="token class-name">String</span> <span class="token function">getMethod</span><span class="token punctuation">(</span><span class="token punctuation">)</span>	
<span class="token comment">// 返回用于发出此请求的HTTP方法的名称，例如GET、POST或PUT。返回的值与CGI变量REQUEST_METHOD的值相同。</span>
<span class="token class-name">String</span> <span class="token function">getPathInfo</span><span class="token punctuation">(</span><span class="token punctuation">)</span>	
<span class="token comment">// 返回与客户端发出此请求时发送的URL相关联的额外路径信息。额外路径信息位于servlet路径之后但在查询字符串之前，并且将以“/”字符开头。</span>
<span class="token class-name">String</span> <span class="token function">getPathTranslated</span><span class="token punctuation">(</span><span class="token punctuation">)</span>	
<span class="token comment">// 返回在servlet名称之后但在查询字符串之前的额外路径信息，并将它转换为实际路径。返回的值与CGI变量PATH_TRANSLATED的值相同。</span>
<span class="token class-name">String</span> <span class="token function">getContextPath</span><span class="token punctuation">(</span><span class="token punctuation">)</span>	
<span class="token comment">// 返回请求URI指示请求上下文的那一部分。请求URI中首先出现的总是上下文路径。路径以“/”字符开头但不以“/”字符结束。对于默认（根）上下文中的servlet，此方法返回“”。容器不会解码此字符串。</span>
<span class="token class-name">String</span> <span class="token function">getQueryString</span><span class="token punctuation">(</span><span class="token punctuation">)</span>	
<span class="token comment">// 返回包含在请求URL中路径后面的查询字符串。如果URL没有查询字符串，则此方法返回null。返回的值与CGI变量QUERY_STRING的值相同。</span>
<span class="token class-name">String</span> <span class="token function">getRequestedSessionId</span><span class="token punctuation">(</span><span class="token punctuation">)</span>	
<span class="token comment">// 返回客户端指定的会话ID。此值可能不同于此请求的当前有效会话的ID。如果客户端没有指定会话ID，则此方法返回null。</span>
<span class="token class-name">String</span> <span class="token function">getRequestURL</span><span class="token punctuation">(</span><span class="token punctuation">)</span>	
<span class="token comment">// 重新构造客户端用于发出请求的URL。返回的URL包含一个协议、服务器名称、端口号、服务器路径，但是不包含查询字符串参数。</span>
<span class="token class-name">String</span> <span class="token function">getServletPath</span><span class="token punctuation">(</span><span class="token punctuation">)</span>	
<span class="token comment">// 返回此请求调用servlet的URL部分。此路径以“/”字符开头，包括servlet名称或到servlet的路径，但不包括任何额外路径信息或查询字符串。返回的值与CGI变量SCRIPT_NAME的值相同。</span>
<span class="token class-name">HttpSession</span>	<span class="token function">getSession</span><span class="token punctuation">(</span><span class="token keyword">boolean</span> create<span class="token punctuation">)</span>	
<span class="token comment">// 返回与此请求关联的当前HttpSession，如果没有当前会话并且create为true，则返回一个新会话。</span>
<span class="token class-name">HttpSession</span>	<span class="token function">getSession</span><span class="token punctuation">(</span><span class="token punctuation">)</span>	
<span class="token comment">// 返回与此请求关联的当前会话，如果该请求没有会话，则创建一个会话。</span>
<span class="token keyword">boolean</span>	<span class="token function">isRequestedSessionIdValid</span><span class="token punctuation">(</span><span class="token punctuation">)</span>	
<span class="token comment">// 检查请求的会话ID是否仍然有效。</span>
<span class="token keyword">boolean</span>	<span class="token function">isRequestedSessionIdFromCookie</span><span class="token punctuation">(</span><span class="token punctuation">)</span>	
<span class="token comment">// 检查请求的会话ID是否是作为cookie进入的。</span>
<span class="token keyword">boolean</span>	<span class="token function">isRequestedSessionIdFromURL</span><span class="token punctuation">(</span><span class="token punctuation">)</span>	
<span class="token comment">// 检查请求的会话ID是否是作为请求URL的一部分进入的。</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h4 id="解决会话重放攻击demo"><a href="#解决会话重放攻击demo" class="headerlink" title="解决会话重放攻击demo"></a>解决会话重放攻击demo</h4><pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token keyword">var</span> random <span class="token operator">=</span> Math<span class="token punctuation">.</span><span class="token function">random</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">*</span><span class="token number">1000000000000000</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">String</span> random <span class="token operator">=</span> request<span class="token punctuation">.</span><span class="token function">getParameter</span><span class="token punctuation">(</span><span class="token string">"random"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">List</span> list <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token class-name">ArrayList</span><span class="token punctuation">)</span>request<span class="token punctuation">.</span><span class="token function">getSession</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getAttribute</span><span class="token punctuation">(</span><span class="token string">"randoms"</span><span class="token punctuation">)</span>
<span class="token keyword">if</span><span class="token punctuation">(</span>list<span class="token punctuation">.</span><span class="token function">contains</span><span class="token punctuation">(</span>random<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
	<span class="token keyword">return</span> <span class="token keyword">null</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>
list<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span>random<span class="token punctuation">)</span><span class="token punctuation">;</span>
request<span class="token punctuation">.</span><span class="token function">getSession</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">setAttribute</span><span class="token punctuation">(</span><span class="token string">"randoms"</span><span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p>处理流程:请求带一个随机数，后台从session中取出存放这个随机数的list，判断list里面是否包含这个随机数，如果包含，证明请求被重复发送，不做处理，如果不包含，则证明是第一次请求，将随机数放进list放入session里面，之后处理业务逻辑</p>
<h3 id="前端相关"><a href="#前端相关" class="headerlink" title="前端相关"></a>前端相关</h3><h4 id="jsp获取session数据"><a href="#jsp获取session数据" class="headerlink" title="jsp获取session数据"></a>jsp获取session数据</h4><pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token operator">&lt;</span><span class="token operator">%=</span>session<span class="token punctuation">.</span><span class="token function">getAttribute</span><span class="token punctuation">(</span><span class="token string">"name"</span><span class="token punctuation">)</span><span class="token operator">%</span><span class="token operator">></span>
or
$<span class="token punctuation">&#123;</span>sessionScope<span class="token punctuation">.</span>name<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>

<h4 id="easyui-combobox下拉框设置checkbox全选"><a href="#easyui-combobox下拉框设置checkbox全选" class="headerlink" title="easyui combobox下拉框设置checkbox全选"></a>easyui combobox下拉框设置checkbox全选</h4><pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token keyword">function</span> <span class="token function">initCombobox</span><span class="token punctuation">(</span><span class="token parameter">id<span class="token punctuation">,</span>data</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
	<span class="token keyword">var</span> value <span class="token operator">=</span> <span class="token string">""</span><span class="token punctuation">;</span>
	<span class="token comment">//加载下拉框复选框</span>
	<span class="token function">$</span><span class="token punctuation">(</span><span class="token string">'#'</span><span class="token operator">+</span>id<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">combobox</span><span class="token punctuation">(</span><span class="token punctuation">&#123;</span>
        <span class="token literal-property property">data</span><span class="token operator">:</span>data<span class="token punctuation">,</span> <span class="token comment">//后台获取下拉框数据的url</span>
        <span class="token literal-property property">method</span><span class="token operator">:</span><span class="token string">'post'</span><span class="token punctuation">,</span>
        <span class="token literal-property property">panelHeight</span><span class="token operator">:</span><span class="token number">200</span><span class="token punctuation">,</span><span class="token comment">//设置为固定高度，combobox出现竖直滚动条</span>
        <span class="token literal-property property">valueField</span><span class="token operator">:</span><span class="token string">'id'</span><span class="token punctuation">,</span>
        <span class="token literal-property property">textField</span><span class="token operator">:</span><span class="token string">'text'</span><span class="token punctuation">,</span>
        <span class="token literal-property property">multiple</span><span class="token operator">:</span><span class="token boolean">true</span><span class="token punctuation">,</span>
        <span class="token literal-property property">editable</span><span class="token operator">:</span><span class="token boolean">false</span><span class="token punctuation">,</span>
        <span class="token function-variable function">formatter</span><span class="token operator">:</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token parameter">row</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span> <span class="token comment">//formatter方法就是实现了在每个下拉选项前面增加checkbox框的方法</span>
            <span class="token keyword">var</span> opts <span class="token operator">=</span> <span class="token function">$</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">combobox</span><span class="token punctuation">(</span><span class="token string">'options'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword">var</span> checkbox_id <span class="token operator">=</span> row<span class="token punctuation">.</span>check_id<span class="token punctuation">;</span>
            <span class="token keyword">if</span><span class="token punctuation">(</span>checkbox_id<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
            	<span class="token keyword">return</span> <span class="token string">'&lt;input type="checkbox" class="combobox-checkbox" id="'</span> <span class="token operator">+</span> checkbox_id <span class="token operator">+</span><span class="token string">'">'</span> <span class="token operator">+</span> row<span class="token punctuation">[</span>opts<span class="token punctuation">.</span>textField<span class="token punctuation">]</span>
            <span class="token punctuation">&#125;</span>
            <span class="token keyword">return</span> <span class="token string">'&lt;input type="checkbox" class="combobox-checkbox">'</span> <span class="token operator">+</span> row<span class="token punctuation">[</span>opts<span class="token punctuation">.</span>textField<span class="token punctuation">]</span>
        <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
        <span class="token function-variable function">onLoadSuccess</span><span class="token operator">:</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span><span class="token comment">//下拉框数据加载成功调用</span>
            <span class="token keyword">var</span> opts <span class="token operator">=</span> <span class="token function">$</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">combobox</span><span class="token punctuation">(</span><span class="token string">'options'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword">var</span> target <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">;</span>
            <span class="token keyword">var</span> values <span class="token operator">=</span> <span class="token function">$</span><span class="token punctuation">(</span>target<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">combobox</span><span class="token punctuation">(</span><span class="token string">'getValues'</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//获取选中的值的values</span>
            $<span class="token punctuation">.</span><span class="token function">map</span><span class="token punctuation">(</span>values<span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token parameter">value</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
                <span class="token keyword">var</span> el <span class="token operator">=</span> opts<span class="token punctuation">.</span>finder<span class="token punctuation">.</span><span class="token function">getEl</span><span class="token punctuation">(</span>target<span class="token punctuation">,</span> value<span class="token punctuation">)</span><span class="token punctuation">;</span>
                el<span class="token punctuation">.</span><span class="token function">find</span><span class="token punctuation">(</span><span class="token string">'input.combobox-checkbox'</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">_propAttr</span><span class="token punctuation">(</span><span class="token string">'checked'</span><span class="token punctuation">,</span> <span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">;</span> 
            <span class="token punctuation">&#125;</span><span class="token punctuation">)</span>
        <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
        <span class="token function-variable function">onSelect</span><span class="token operator">:</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token parameter">row</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span> <span class="token comment">//选中一个选项时调用</span>
        	<span class="token keyword">var</span> opts <span class="token operator">=</span> <span class="token function">$</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">combobox</span><span class="token punctuation">(</span><span class="token string">'options'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token comment">//当点击所有时，则勾中所有的选项</span>
            <span class="token keyword">if</span> <span class="token punctuation">(</span>row<span class="token punctuation">.</span>text <span class="token operator">===</span> <span class="token string">"全选"</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
            	<span class="token keyword">var</span> data <span class="token operator">=</span> <span class="token function">$</span><span class="token punctuation">(</span><span class="token string">"#"</span><span class="token operator">+</span>id<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">combobox</span><span class="token punctuation">(</span><span class="token string">'getData'</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">;</span>
            	<span class="token keyword">var</span> show_type <span class="token operator">=</span> <span class="token function">$</span><span class="token punctuation">(</span><span class="token string">"#show_type"</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">combobox</span><span class="token punctuation">(</span><span class="token string">'getValue'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">var</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> data<span class="token punctuation">.</span>length<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
                	<span class="token comment">//获取选中的值的values</span>
                    <span class="token function">$</span><span class="token punctuation">(</span><span class="token string">"#"</span><span class="token operator">+</span>id<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">val</span><span class="token punctuation">(</span><span class="token function">$</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">combobox</span><span class="token punctuation">(</span><span class="token string">'getValues'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                    <span class="token comment">// 选择全部和按照机组展示全选时才选中发电类型 机组类型 供热状态 否则不选择这三个</span>
                	<span class="token keyword">if</span><span class="token punctuation">(</span>show_type <span class="token operator">!=</span> <span class="token string">'6'</span> <span class="token operator">&amp;&amp;</span> show_type <span class="token operator">!=</span> <span class="token string">''</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
                		<span class="token keyword">var</span> field <span class="token operator">=</span> data<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span>opts<span class="token punctuation">.</span>valueField<span class="token punctuation">]</span><span class="token punctuation">;</span>
                		<span class="token keyword">if</span><span class="token punctuation">(</span>field <span class="token operator">==</span> <span class="token string">'fdlx'</span> <span class="token operator">||</span> field <span class="token operator">==</span> <span class="token string">'grqk'</span> <span class="token operator">||</span> field <span class="token operator">==</span> <span class="token string">'unit_status'</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
                			<span class="token keyword">continue</span><span class="token punctuation">;</span>
                		<span class="token punctuation">&#125;</span>
                	<span class="token punctuation">&#125;</span>
        		   <span class="token comment">//设置选中值所对应的复选框为选中状态</span>
                    <span class="token keyword">var</span> el <span class="token operator">=</span> opts<span class="token punctuation">.</span>finder<span class="token punctuation">.</span><span class="token function">getEl</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">,</span> data<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span>opts<span class="token punctuation">.</span>valueField<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                    el<span class="token punctuation">.</span><span class="token function">find</span><span class="token punctuation">(</span><span class="token string">'input.combobox-checkbox'</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">_propAttr</span><span class="token punctuation">(</span><span class="token string">'checked'</span><span class="token punctuation">,</span> <span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token punctuation">&#125;</span>
                <span class="token keyword">var</span> list <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
                $<span class="token punctuation">.</span><span class="token function">map</span><span class="token punctuation">(</span>opts<span class="token punctuation">.</span>data<span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token parameter">opt</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>	
                	<span class="token comment">// 选择全部和按照机组展示全选时才选中发电类型 机组类型 供热状态 否则不选择这三个</span>
                	<span class="token keyword">if</span><span class="token punctuation">(</span>show_type <span class="token operator">!=</span> <span class="token string">'6'</span> <span class="token operator">&amp;&amp;</span> show_type <span class="token operator">!=</span> <span class="token string">''</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>	
                		<span class="token keyword">if</span><span class="token punctuation">(</span>opt<span class="token punctuation">.</span>id <span class="token operator">!=</span> <span class="token string">'fdlx'</span> <span class="token operator">&amp;&amp;</span> opt<span class="token punctuation">.</span>id <span class="token operator">!=</span> <span class="token string">'grqk'</span> <span class="token operator">&amp;&amp;</span> opt<span class="token punctuation">.</span>id <span class="token operator">!=</span> <span class="token string">'unit_status'</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
                			list<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>opt<span class="token punctuation">.</span>id<span class="token punctuation">)</span><span class="token punctuation">;</span>
                		<span class="token punctuation">&#125;</span>
                	<span class="token punctuation">&#125;</span><span class="token keyword">else</span><span class="token punctuation">&#123;</span>
                		list<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>opt<span class="token punctuation">.</span>id<span class="token punctuation">)</span><span class="token punctuation">;</span>
                	<span class="token punctuation">&#125;</span>
                    
                <span class="token punctuation">&#125;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token function">$</span><span class="token punctuation">(</span><span class="token string">"#"</span><span class="token operator">+</span>id<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">combobox</span><span class="token punctuation">(</span><span class="token string">'setValues'</span><span class="token punctuation">,</span> list<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// combobox全选</span>
            
            <span class="token punctuation">&#125;</span> <span class="token keyword">else</span> <span class="token punctuation">&#123;</span>
                <span class="token comment">//获取选中的值的values</span>
                <span class="token function">$</span><span class="token punctuation">(</span><span class="token string">"#"</span><span class="token operator">+</span>id<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">val</span><span class="token punctuation">(</span><span class="token function">$</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">combobox</span><span class="token punctuation">(</span><span class="token string">'getValues'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    		   <span class="token comment">//设置选中值所对应的复选框为选中状态</span>
                <span class="token keyword">var</span> el <span class="token operator">=</span> opts<span class="token punctuation">.</span>finder<span class="token punctuation">.</span><span class="token function">getEl</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">,</span> row<span class="token punctuation">[</span>opts<span class="token punctuation">.</span>valueField<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                el<span class="token punctuation">.</span><span class="token function">find</span><span class="token punctuation">(</span><span class="token string">'input.combobox-checkbox'</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">_propAttr</span><span class="token punctuation">(</span><span class="token string">'checked'</span><span class="token punctuation">,</span> <span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">&#125;</span>

        <span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
        <span class="token function-variable function">onUnselect</span><span class="token operator">:</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token parameter">row</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span><span class="token comment">//不选中一个选项时调用</span>
            <span class="token keyword">var</span> opts <span class="token operator">=</span> <span class="token function">$</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">combobox</span><span class="token punctuation">(</span><span class="token string">'options'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword">if</span> <span class="token punctuation">(</span>row<span class="token punctuation">.</span>text <span class="token operator">===</span> <span class="token string">"全选"</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
                <span class="token keyword">var</span> a <span class="token operator">=</span> <span class="token function">$</span><span class="token punctuation">(</span><span class="token string">"#"</span><span class="token operator">+</span>id<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">combobox</span><span class="token punctuation">(</span><span class="token string">'getData'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">var</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> a<span class="token punctuation">.</span>length<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
                	 <span class="token function">$</span><span class="token punctuation">(</span><span class="token string">"#"</span><span class="token operator">+</span>id<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">val</span><span class="token punctuation">(</span><span class="token function">$</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">combobox</span><span class="token punctuation">(</span><span class="token string">'getValues'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                     <span class="token keyword">var</span> el <span class="token operator">=</span> opts<span class="token punctuation">.</span>finder<span class="token punctuation">.</span><span class="token function">getEl</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">,</span> data<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span>opts<span class="token punctuation">.</span>valueField<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                     el<span class="token punctuation">.</span><span class="token function">find</span><span class="token punctuation">(</span><span class="token string">'input.combobox-checkbox'</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">_propAttr</span><span class="token punctuation">(</span><span class="token string">'checked'</span><span class="token punctuation">,</span> <span class="token boolean">false</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token punctuation">&#125;</span>
                <span class="token function">$</span><span class="token punctuation">(</span><span class="token string">"#"</span><span class="token operator">+</span>id<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">combobox</span><span class="token punctuation">(</span><span class="token string">'clear'</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//清空选中项</span>
            <span class="token punctuation">&#125;</span> <span class="token keyword">else</span> <span class="token punctuation">&#123;</span>
            	 <span class="token comment">//获取选中的值的values</span>
                <span class="token function">$</span><span class="token punctuation">(</span><span class="token string">"#"</span><span class="token operator">+</span>id<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">val</span><span class="token punctuation">(</span><span class="token function">$</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">combobox</span><span class="token punctuation">(</span><span class="token string">'getValues'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token keyword">var</span> el <span class="token operator">=</span> opts<span class="token punctuation">.</span>finder<span class="token punctuation">.</span><span class="token function">getEl</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">,</span> row<span class="token punctuation">[</span>opts<span class="token punctuation">.</span>valueField<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                el<span class="token punctuation">.</span><span class="token function">find</span><span class="token punctuation">(</span><span class="token string">'input.combobox-checkbox'</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">_propAttr</span><span class="token punctuation">(</span><span class="token string">'checked'</span><span class="token punctuation">,</span> <span class="token boolean">false</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

            <span class="token punctuation">&#125;</span>
        <span class="token punctuation">&#125;</span>
    <span class="token punctuation">&#125;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h3 id="其他"><a href="#其他" class="headerlink" title="其他"></a>其他</h3><h4 id="二叉树遍历"><a href="#二叉树遍历" class="headerlink" title="二叉树遍历"></a>二叉树遍历</h4><p>前序遍历A-B-D-F-G-H-I-E-C<br>中序遍历F-D-H-G-I-B-E-A-C<br>后序遍历F-H-I-G-D-E-B-C-A<br>前序(根左右)，中序(左根右)，后序(左右根)</p>
<p><img src="/blog/images/ecs.jpg"></p>
<h4 id="py-import"><a href="#py-import" class="headerlink" title="py import"></a>py import</h4><pre class="line-numbers language-python" data-language="python"><code class="language-python"><span class="token keyword">import</span> math
<span class="token keyword">print</span> math<span class="token punctuation">.</span>pi
<span class="token comment"># 等价于</span>
<span class="token keyword">from</span> math <span class="token keyword">import</span> pi
<span class="token keyword">print</span> pi<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h4 id="excel查看路径和文件名"><a href="#excel查看路径和文件名" class="headerlink" title="excel查看路径和文件名"></a>excel查看路径和文件名</h4><pre class="line-numbers language-none"><code class="language-none">在任一单元格输入&#x3D;CELL(&quot;filename&quot;)即可<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>

<h4 id="windows杀掉进程"><a href="#windows杀掉进程" class="headerlink" title="windows杀掉进程"></a>windows杀掉进程</h4><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">taskkill /pid pid
<span class="token comment"># /f 强制</span>
<span class="token comment"># /t 终止进程和他启用的子进程</span>
taskkill /? <span class="token comment"># 查看相关用法</span>
tasklist <span class="token comment"># 查看所有进程</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h4 id="cron表达式"><a href="#cron表达式" class="headerlink" title="cron表达式"></a>cron表达式</h4><p>3&#x2F;12 第三分钟开始,每12分钟触发<br>12,15,17 第12分钟、15分钟、17分钟触发<br>3-19 看使用在哪个域 如果在minute域则表示在第3分钟到第19分钟每分钟触发一次 在second域则表示从第3秒到第19秒每秒触发一次<br>24 43 2 ? 1-12 3 * 每周三2点43分24秒触发一次</p>
<h4 id="PS相关操作"><a href="#PS相关操作" class="headerlink" title="PS相关操作"></a>PS相关操作</h4><p><strong>换证件照底色</strong></p>
<p>打开图片-选择主体-选择并遮住-调整下细节-输出带有蒙版图层-新建图层(不是(ctrl+j)复制图层)- ctrl+del(填充背景色)或者alt+del(填充前景色)-调整图层位置-完成。其中蓝色背景的色号一般为：#0033A0（深蓝色），RGB色值为R:67, G:142, B:2191。白色背景的色号是：#FFFFFF（纯白色），RGB色值为R:255, G:255, B:2551。红色背景的色号可以为：#C8102E（标准红色），RGB色值为R:255, G:0, B:01.</p>
<blockquote>
<p><a target="_blank" rel="noopener" href="https://baijiahao.baidu.com/s?id=1709949846668174480&wfr=spider&for=pc">3种PS更改证件照底色的方法</a></p>
</blockquote>
<p><strong>更换大小眼</strong><br>打开图片-ctrl+j复制图层-套索工具选中眼睛-移到小眼睛位置-右键水平翻转-调整-右上角对号应用</p>
<p><strong>瘦脸</strong><br>滤镜-液化</p>
<p><strong>去水印</strong></p>
<ol>
<li>矩形选框工具选中水印选择“编辑”-“填充”（快捷键：Shift+F5），属性选“内容识别”点击“确定”，取消选区。</li>
<li>选择“污点修复画笔工具”</li>
</ol>
<h2 id="进制转换"><a href="#进制转换" class="headerlink" title="进制转换"></a>进制转换</h2><h3 id="十进制转换二进制"><a href="#十进制转换二进制" class="headerlink" title="十进制转换二进制"></a>十进制转换二进制</h3><p>一直除以2最后的商+余数开始排序</p>
<p>5&#x2F;2&#x3D;2余1，2&#x2F;2 &#x3D;1余0，所以5的二进制为101</p>
<p>6&#x2F;2&#x3D;3余0，3&#x2F;2&#x3D;1余1，所以6的2进制是110</p>
<h3 id="二进制转换十进制"><a href="#二进制转换十进制" class="headerlink" title="二进制转换十进制"></a>二进制转换十进制</h3><p>1         0          1</p>
<p>1* 2的2次方 + 0 * 2的1次方 + 1 * 2 的0次方 &#x3D; 5</p>
<h3 id="运算符"><a href="#运算符" class="headerlink" title="运算符"></a>运算符</h3><p><strong>“&lt;&lt;”左移运算符</strong></p>
<p><strong>转换为二进制后左移 例:2&lt;&lt;3 即2的二进制左移三位</strong></p>
<p>2的二进制为2&#x2F;2&#x3D;1余0 ， 10左移三位后补0 （00000010）（00010000）</p>
<p>n&lt;&lt;3 可以转换为 n * 2 ^ 3</p>
<p><strong>“&gt;&gt;”右移运算符</strong></p>
<p><strong>转换为二进制后右移 例:8&gt;&gt;3 即8的二进制右移三位</strong></p>
<p>8的二进制为8&#x2F;2&#x3D;4余0 ,4&#x2F;2&#x3D;2余0, 2&#x2F;2&#x3D;1余0, 1000右移三位（删除后三位） （0000001000）（000000001）为1</p>
<p>n&gt;&gt;3 可以转换为 n&#x2F;2^3</p>
<p><strong>&amp; 运算符</strong></p>
<p>如果相对应位都是1，则结果为1，否则为0</p>
<p><strong>| 运算符</strong></p>
<p>如果相对应位都是0，则结果为0，否则为1</p>
<p>** ^ 运算符**</p>
<p>如果相对应位值相同，则结果为0，否则为1</p>
<p><strong>比较</strong></p>
<p>^: 可以不借助第三块空间的方式交换两个变量的值<br>&amp;: 在某些情况下可以取代%的运算</p>
<p>某些情况：当我们拿着一个正数去%上2的n次方数的时候,其实结果等价于我们拿着这个正数&amp;上2的n次方数-1的结果</p>
<p>x % 2(n) &#x3D;&#x3D; x &amp; 2(n)-1</p>
<p>17 % 4 &#x3D;&#x3D; 17 &amp; 3</p>
<p>99 % 64 &#x3D;&#x3D; 99 &amp; 63</p>
<blockquote>
<p><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/INYjYfwhPU7uZcNmzBTpNQ">你可能不知道的位运算技巧</a></p>
</blockquote>
<h2 id="数据库相关知识点"><a href="#数据库相关知识点" class="headerlink" title="数据库相关知识点"></a>数据库相关知识点</h2><h3 id="SQL查询慢的原因"><a href="#SQL查询慢的原因" class="headerlink" title="SQL查询慢的原因"></a>SQL查询慢的原因</h3><ol>
<li>sql没加索引</li>
<li>sql索引不生效:<ul>
<li>隐式的类型转换，索引失效,</li>
<li>查询条件包含or，可能导致索引失效,</li>
<li>like通配符可能导致索引失效,</li>
<li>查询条件不满足联合索引的最左匹配原则,</li>
<li>在索引列上使用mysql的内置函数,</li>
<li>对索引进行列运算（如，+、-、*、&#x2F;）,索引不生效,</li>
<li>索引字段上使用（!&#x3D;或者&lt;&gt;），索引可能失效,</li>
<li>索引字段上使用is null,is not null，索引可能失效,</li>
<li>左右连接，关联的字段编码格式不一样,</li>
<li>优化器选错了索引</li>
</ul>
</li>
<li>limit深分页问题</li>
<li>单表数据量太大</li>
<li>join或者子查询过多</li>
<li>in元素过多</li>
<li>数据库在刷脏页</li>
<li>order by文件排序</li>
<li>拿不到锁</li>
<li>delete + in子查询不走索引！</li>
<li>group by使用临时表</li>
<li>系统硬件或网络资源</li>
</ol>
<blockquote>
<p><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/qCA7hICMktxeYO7Jc0oVVg">盘点MySQL慢查询的12个原因</a></p>
</blockquote>
<h3 id="SQL优化"><a href="#SQL优化" class="headerlink" title="SQL优化"></a>SQL优化</h3><ol>
<li><p>查询SQL尽量不要使用<code>select *</code>，而是select具体字段。</p>
<blockquote>
<p><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/2ActOr3bivQSMdyIZA5mbw">为啥不建议使用Select *？</a></p>
</blockquote>
</li>
<li><p>如果知道查询结果只有一条或者只要最大&#x2F;最小一条记录，建议用limit 1</p>
</li>
<li><p>应尽量避免在where子句中使用or来连接条件</p>
</li>
<li><p>优化limit分页</p>
</li>
<li><p>优化你的like语句</p>
</li>
<li><p>使用where条件限定要查询的数据，避免返回多余的行</p>
</li>
<li><p>尽量避免在索引列上使用mysql的内置函数</p>
</li>
<li><p>应尽量避免在where子句中对字段进行表达式操作，这将导致系统放弃使用索引而进行全表扫</p>
</li>
<li><p>Inner join 、left join、right join，优先使用Inner join，如果是left join，左边表结果尽量小</p>
</li>
<li><p>应尽量避免在where子句中使用!&#x3D;或&lt;&gt;操作符，否则将引擎放弃使用索引而进行全表扫描。</p>
</li>
<li><p>使用联合索引时，注意索引列的顺序，一般遵循最左匹配原则。</p>
</li>
<li><p>对查询进行优化，应考虑在where及order by涉及的列上建立索引，尽量避免全表扫描。</p>
</li>
<li><p>如果插入数据过多，考虑批量插入。</p>
</li>
<li><p>在适当的时候，使用覆盖索引。</p>
</li>
<li><p>慎用distinct关键字</p>
</li>
<li><p>删除冗余和重复索引</p>
</li>
<li><p>如果数据量较大，优化你的修改&#x2F;删除语句。</p>
</li>
<li><p>where子句中考虑使用默认值代替null。</p>
</li>
<li><p>不要有超过5个以上的表连接</p>
</li>
<li><p>exist&amp;in的合理利用</p>
</li>
<li><p>尽量用union all替换union</p>
</li>
<li><p>索引不宜太多，一般5个以内。</p>
</li>
<li><p>尽量使用数字型字段，若只含数值信息的字段尽量不要设计为字符型</p>
</li>
<li><p>索引不适合建在有大量重复数据的字段上，如性别这类型数据库字段。</p>
</li>
<li><p>尽量避免向客户端返回过多数据量。</p>
</li>
<li><p>当在SQL语句中连接多个表时,请使用表的别名，并把别名前缀于每一列上，这样语义更加清晰</p>
</li>
<li><p>尽可能使用varchar&#x2F;nvarchar代替char&#x2F;nchar。</p>
</li>
<li><p>为了提高group by语句的效率，可以在执行到该语句前，把不需要的记录过滤掉。</p>
</li>
<li><p>如果字段类型是字符串，where时一定用引号括起来，否则索引失效</p>
</li>
<li><p>使用explain分析你SQL的计划</p>
</li>
</ol>
<table>
<thead>
<tr>
<th align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/5zsHr2tKOOkRN6mFa65tvw">SQL优化的N种方法</a></th>
<th align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/4ByWZVc5jVmuVcFqkkIC_A">吐血整理！书写高质量SQL的30条建议</a></th>
<th align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/Ky_jak-u7vCX4vXkAONN_w">MySQL数据量太大优化方案</a></th>
</tr>
</thead>
<tbody><tr>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/qJkEgNRlUwBAe41IdtJ_hQ">Mysql数据库查询好慢，除了索引，还能因为什么？</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/eoKViDLmAB3ZaPuGgsk_zQ">MySQL常用优化指南，面试再也不怕了！</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/Q9yb1Aa-PQCW1DguRzLUdg">深入理解为什么MySQL全表扫描很慢？</a></td>
</tr>
<tr>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/Ow1vuBST7YUh5J5SYHT6dg">专业解决MySQL查询速度慢与性能差！</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/VmAD31GYPV8HcE-m2aIrnA">记一次神奇的SQL查询经历，group by慢查询优化</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/9RJxCqpKt0LTaSG0SHIttA">SQL子查询怎么优化？写的很深！</a></td>
</tr>
<tr>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/NS2trUx9nVN5gNquVVbruw">SQL优化极简法则，还有谁不会？</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/Z1OOMWBqeF_QQBvhbwo-sw">如何写出一手好SQL？很有必要！</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/9LIn8H78gNyYnKZrIHLbgA">每个后端都应该知道的八个提升SQL性能的Tips</a></td>
</tr>
<tr>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/BgOFpBBVvvlx-Jq0ZQg3AQ">10个经典场景带你玩转SQL优化</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/Cirhr8SmBzyllAI3nQTXag">聊聊sql优化的15个小技巧</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/ERpnzP4UvS-RUS2cmGmxKg">阿里一面：SQL优化有哪些技巧？</a></td>
</tr>
<tr>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/w5oy8tg8sQ3GB8hw4esVDw">如何让JOIN跑得更快？</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/wjDNsGFYxfgK5oAn9kdN9Q">SQL优化万能公式：5大步骤+10个案例</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/A8OM449_JhKPIZa4P3w08Q">SQL优化21连击+思维导图</a></td>
</tr>
</tbody></table>
<h3 id="水平拆分、垂直拆分"><a href="#水平拆分、垂直拆分" class="headerlink" title="水平拆分、垂直拆分"></a>水平拆分、垂直拆分</h3><ul>
<li>水平拆分，就是把一个表的数据给弄到多个库的多个表里去，但是每个库的表结构都一样，只不过每个库表放的数据是不同的，所有库表的数据加起来就是全部数据。水平拆分的意义，就是将数据均匀放更多的库里，然后用多个库来扛更高的并发，还有就是用多个库的存储容量来进行扩容。</li>
<li>垂直拆分，就是把一个有很多字段的表给拆分成多个表，或者是多个库上去。每个库表的结构都不一样，每个库表都包含部分字段。一般来说，会将较少的访问频率很高的字段放到一个表里去，然后将较多的访问频率很低的字段放到另外一个表里去。因为数据库是有缓存的，你访问频率高的行字段越少，就可以在缓存里缓存更多的行，性能就越好。这个一般在表层面做的较多一些。</li>
</ul>
<table>
<thead>
<tr>
<th align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/_wDGx0UfG2hXo80JvvpBAw">分库分表的四个面时连环炮问题</a></th>
<th align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/jqry9LnCVBt64d_DdpRXJg">这四种情况下，才是考虑分库分表的时候！</a></th>
<th align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/fbZ8SpHh8ScV8Xt50PGb6A">分库分表？如何做到永不迁移数据和避免热点？</a></th>
</tr>
</thead>
<tbody><tr>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/4eII2YyVA_snLn4kswtNcQ">这应该是最详尽的MySQL分库分表文章了</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/Q6VbgQaz0NVFG-8L4GC7bQ">好好的系统，为什么要分库分表？</a></td>
<td align="center"></td>
</tr>
</tbody></table>
<h3 id="主键、外键"><a href="#主键、外键" class="headerlink" title="主键、外键"></a>主键、外键</h3><ul>
<li><strong>主键（主码）</strong>：主键用于唯一标识一个元组，不能有重复，不允许为空。一个表只能有一个主键。</li>
<li><strong>外键（外码）</strong>：外键用来和其他表建立联系用，外键是另一表的主键，外键是可以有重复的，可以是空值。一个表可以有多个外键。</li>
</ul>
<p><strong>为什么不推荐使用外键与级联？</strong></p>
<p>对于外键和级联，阿里巴巴开发手册这样说到：</p>
<blockquote>
<p>【强制】不得使用外键与级联，一切外键概念必须在应用层解决。</p>
<p>说明:以学生和成绩的关系为例，学生表中的student_id是主键，那么成绩表中的student_id则为外键。如果更新学生表中的student_id，同时触发成绩表中的student_id更新，即为级联更新。外键与级联更新适用于单机低并发，不适合分布式、高并发集群;级联更新是强阻塞，存在数据库更新风暴的风险;外键影响数据库的插入速度</p>
</blockquote>
<p>为什么不要用外键呢？大部分人可能会这样回答：</p>
<ol>
<li><strong>增加了复杂性</strong>：a.每次做DELETE或者UPDATE都必须考虑外键约束，会导致开发的时候很痛苦,测试数据极为不方便;b.外键的主从关系是定的，假如那天需求有变化，数据库中的这个字段根本不需要和其他表有关联的话就会增加很多麻烦。</li>
<li><strong>增加了额外工作</strong>：数据库需要增加维护外键的工作，比如当我们做一些涉及外键字段的增，删，更新操作之后，需要触发相关操作去检查，保证数据的的一致性和正确性，这样会不得不消耗资源；（个人觉得这个不是不用外键的原因，因为即使你不使用外键，你在应用层面也还是要保证的。所以，我觉得这个影响可以忽略不计。）</li>
<li><strong>对分库分表不友好</strong>：因为分库分表下外键是无法生效的。</li>
<li>……</li>
</ol>
<p>我个人觉得上面这种回答不是特别的全面，只是说了外键存在的一个常见的问题。实际上，我们知道外键也是有很多好处的，比如：</p>
<ol>
<li>保证了数据库数据的一致性和完整性；</li>
<li>级联操作方便，减轻了程序代码量；</li>
<li>……</li>
</ol>
<p>所以说，不要一股脑的就抛弃了外键这个概念，既然它存在就有它存在的道理，如果系统不涉及分库分表，并发量不是很高的情况还是可以考虑使用外键的。</p>
<p><strong>关于数据库外键是否应该使用</strong></p>
<p>外键提供的几种在更新和删除时的不同行为都可以帮助我们保证数据库中数据的一致性和引用合法性，但是外键的使用也需要数据库承担额外的开销，在大多数服务都可以水平扩容的今天，高并发场景中使用外键确实会影响服务的吞吐量上限。在数据库之外手动实现外键的功能是可能的，但是却会带来很多维护上的成本或者需要我们在数据一致性上做出一些妥协。我们可以从可用性、一致性几个方面分析使用外键、模拟外键以及不使用外键的差异：不使用外键牺牲了数据库中数据的一致性，但是却能够减少数据库的负载；模拟外键将一部分工作移到了数据库之外，我们可能需要放弃一部分一致性以获得更高的可用性，但是为了这部分可用性，我们会付出更多的研发与维护成本，也增加了与数据库之间的网络通信次数；使用外键保证了数据库中数据的一致性，也将全部的计算任务全部交给了数据库；在大多数不需要高并发或者对一致性有较强要求的系统中，我们可以直接使用数据库提供的外键帮助我们对数据进行校验，但是在对一致性要求不高的、复杂的场景或者大规模的团队中，不使用外键也确实可以为数据库减负，而大团队也有更多的时间和精力去设计其他的方案，例如：分布式的关系型数据库。当我们考虑应不应该在数据库中使用外键时，需要关注的核心我们的数据库承担这部分计算任务后会不会影响系统的可用性，在使用时也不应该一刀切的决定用或者不用外键，应该根据具体的场景做决策，我们在这里介绍了两个使用外键时可能遇到的问题：RESTRICT外键会在更新和删除关系表中的数据时对外键约束的合法性进行检查，保证外键不会引用到不存在的记录；CASCADE外键会在更新和删除关系表中的数据时触发对关联记录的更新和删除，在数据量较大的数据库中可能会有数量级的放大效果</p>
<h3 id="数据库三大范式"><a href="#数据库三大范式" class="headerlink" title="数据库三大范式"></a>数据库三大范式</h3><p>数据库范式有3种：</p>
<ul>
<li>1NF(第一范式)：属性不可再分。</li>
<li>2NF(第二范式)：1NF的基础之上，消除了非主属性对于码的部分函数依赖。</li>
<li>3NF(第三范式)：3NF在2NF的基础之上，消除了非主属性对于码的传递函数依赖。</li>
</ul>
<p><strong>1NF(第一范式)</strong></p>
<p>属性（对应于表中的字段）不能再被分割，也就是这个字段只能是一个值，不能再分为多个其他的字段了。<strong>1NF是所有关系型数据库的最基本要求</strong>，也就是说关系型数据库中创建的表一定满足第一范式。</p>
<p><strong>2NF(第二范式)</strong></p>
<p>2NF在1NF的基础之上，消除了非主属性对于码的部分函数依赖。如下图所示，展示了第一范式到第二范式的过渡。第二范式在第一范式的基础上增加了一个列，这个列称为主键，非主属性都依赖于主键。</p>
<p><img src="https://oss.javaguide.cn/github/javaguide/csdn/bd1d31be3779342427fc9e462bf7f05c.png" alt="第二范式"></p>
<p>一些重要的概念：</p>
<ul>
<li><strong>函数依赖（functionaldependency）</strong>：若在一张表中，在属性（或属性组）X的值确定的情况下，必定能确定属性Y的值，那么就可以说Y函数依赖于X，写作X→Y。</li>
<li><strong>部分函数依赖（partialfunctionaldependency）</strong>：如果X→Y，并且存在X的一个真子集X0，使得X0→Y，则称Y对X部分函数依赖。比如学生基本信息表R中（学号，身份证号，姓名）当然学号属性取值是唯一的，在R关系中，（学号，身份证号）-&gt;（姓名），（学号）-&gt;（姓名），（身份证号）-&gt;（姓名）；所以姓名部分函数依赖与（学号，身份证号）；</li>
<li><strong>完全函数依赖（Fullfunctionaldependency）</strong>：在一个关系中，若某个非主属性数据项依赖于全部关键字称之为完全函数依赖。比如学生基本信息表R（学号，班级，姓名）假设不同的班级学号有相同的，班级内学号不能相同，在R关系中，（学号，班级）-&gt;（姓名），但是（学号）-&gt;(姓名)不成立，（班级）-&gt;(姓名)不成立，所以姓名完全函数依赖与（学号，班级）；</li>
<li><strong>传递函数依赖</strong>：在关系模式R(U)中，设X，Y，Z是U的不同的属性子集，如果X确定Y、Y确定Z，且有X不包含Y，Y不确定X，（X∪Y）∩Z&#x3D;空集合，则称Z传递函数依赖(transitivefunctionaldependency)于X。传递函数依赖会导致数据冗余和异常。传递函数依赖的Y和Z子集往往同属于某一个事物，因此可将其合并放到一个表中。比如在关系R(学号,姓名,系名，系主任)中，学号→系名，系名→系主任，所以存在非主属性系主任对于学号的传递函数依赖。。</li>
</ul>
<p><strong>3NF(第三范式)</strong></p>
<p>3NF在2NF的基础之上，消除了非主属性对于码的传递函数依赖。符合3NF要求的数据库设计，基本上解决了数据冗余过大，插入异常，修改异常，删除异常的问题。比如在关系R(学号,姓名,系名，系主任)中，学号→系名，系名→系主任，所以存在非主属性系主任对于学号的传递函数依赖，所以该表的设计，不符合3NF的要求。</p>
<p><strong>总结</strong></p>
<ul>
<li>1NF：数据库表的每一列都是不可分割的基本数据项，同一列中不能有多个值，不能在一列中存放多个属性。例如员工信息表，不能在信息一列中放入电话，住址等信息，应该单独设计成电话、住址各一列</li>
<li>2NF：每个表必须有主键(Primary key),其他数据元素与主键一一对应。通常称这种关系为函数依赖(Functional dependence)关系，即表中其他数据元素都依赖于主键,或称该数据元素惟一地被主键所标识,例如学生表（学生id，姓名，成绩，合格状态），其中合格状态这一列不依赖于学生信息，而依赖于成绩，所以不符合第二范式</li>
<li>3NF:要求一个数据库表中不包含已在其它表中已包含的非主关键字信息,例如学生表（学生id，姓名，班级id，班级位置）班级表(班级id，班级名，班级位置)，学生表里已经有了班级的id，可以推断出班级位置，无需在学生表里存入班级位置信息<br>BCNF:所有非主属性对每一个候选键都是完全函数依赖；所有的主属性对每一个不包含它的候选键，也是完全函数依赖；没有任何属性完全函数依赖于非候选键的任何一组属性</li>
</ul>
<p>注意事项：</p>
<ol>
<li>第二范式与第三范式的本质区别：在于有没有分出两张表。<br>第二范式是说一张表中包含了多种不同实体的属性，那么必须要分成多张表，第三范式是要求已经分好了多张表的话，一张表中只能有另一张标的ID，而不能有其他任何信息，（其他任何信息，一律用主键在另一张表中查询）。</li>
<li>必须先满足第一范式才能满足第二范式，必须同时满足第一第二范式才能满足第三范式。<br>三大范式只是一般设计数据库的基本理念，可以建立冗余较小、结构合理的数据库。如果有特殊情况，当然要特殊对待，数据库设计最重要的是看需求跟性能，需求&gt;性能&gt;表结构。所以不能一味的去追求范式建立数据库。</li>
</ol>
<h3 id="五大约束"><a href="#五大约束" class="headerlink" title="五大约束"></a>五大约束</h3><p>数据库中的五大约束包括：</p>
<ol>
<li>主键约束（Primay Key Coustraint）唯一性，非空性；</li>
<li>唯一约束 （Unique Counstraint）唯一性，可以空，但只能有一个</li>
<li>默认约束 (Default Counstraint)该数据的默认值；</li>
<li>外键约束 (Foreign Key Counstraint)需要建立两表间的关系；</li>
<li>非空约束（Not Null Counstraint）:设置非空约束，该字段不能为空。</li>
</ol>
<h3 id="select-for-update加锁"><a href="#select-for-update加锁" class="headerlink" title="select for update加锁"></a>select for update加锁</h3><p>总结一下select…for update加锁的情况：</p>
<ol>
<li>如果查询条件用了索引&#x2F;主键，那么<code>select ..... for update</code>就会进行行锁。</li>
<li>如果是普通字段(没有索引&#x2F;主键)，那么<code>select ..... for update</code>就会进行锁表</li>
</ol>
<blockquote>
<p>主键字段：加行锁。<br>唯一索引字段：加行锁。<br>普通索引字段：加间隙锁。<br>主键范围：加多个行锁。<br>唯一索引范围，加多个行锁。<br>普通字段：加间隙锁，看着像表锁。。<br>查询空数据：加间隙锁。<br>如果事务1加了行锁，一直没有释放锁，事务2操作相同行的数据时，会一直等待直到超时。<br>如果事务1加了表锁，一直没有释放锁，事务2不管操作的是哪一行数据，都会一直等待直到超时。</p>
</blockquote>
<p>此外，有些小伙伴，可能会好奇，直接执行update语句，也会加行锁，为什么还需要使用for update关键字加行锁呢？<br>答：for update关键字是加在select语句中的，它从查到那行数据开始，直到事务提交，整个过程中都会加锁。<br>而直接执行update语句，是在更新数据的时候加锁，二者有本质的区别。</p>
<h3 id="MySQL中的distinct和group-by哪个效率更高？"><a href="#MySQL中的distinct和group-by哪个效率更高？" class="headerlink" title="MySQL中的distinct和group by哪个效率更高？"></a>MySQL中的distinct和group by哪个效率更高？</h3><p>在语义相同，有索引的情况下：group by和distinct都能使用索引，效率相同。<br>在语义相同，无索引的情况下：distinct效率高于group by。原因是distinct和group by都会进行分组操作，但group by可能会进行排序，触发filesort，导致sql执行效率低下。</p>
<h3 id="相关文章"><a href="#相关文章" class="headerlink" title="相关文章"></a>相关文章</h3><table>
<thead>
<tr>
<th align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/gCT048J16KO3ucNtzvAWxA">数据库面试题（函数和存储过程区别）</a></th>
<th align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/zx2PY8A_L5wnVc7goMY5WQ">吊打面试官？2020年数据库高频面试题</a></th>
<th align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s?__biz=Mzg2MDYzODI5Nw==&mid=2247494238&idx=1&sn=e44bf7e2ba678a562246e8831d5d8dc4&source=41#wechat_redirect">JDBC查询各种姿势（普通、流式、游标）</a></th>
</tr>
</thead>
<tbody><tr>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/AUXNm2LyVl4NtKGEzBaWFg">1.2W字的SQL语法速成手册</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/BhMjFRqlDYKQNaCKj2tnkQ">使用uuid作为数据库主键，被技术总监怼了一顿！</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/zdxkbtHxdgGtgck1zjZmWw">一直认为count(1)比count(*)效果高，被同事鄙视了。</a></td>
</tr>
<tr>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/7ipMLtkeJhCAtxKlEvOFzw">我说用count(*)统计行数，面试官让我回去等消息..</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/Siyd8kkZEsSV5g0D2qsCHg">8种常见SQL错误用法</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/SZnwP5Guu4RraxBI6l6nFg">常见的SQL面试题：经典50例</a></td>
</tr>
<tr>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/NbZnNOZTUZaavEAKD-CSYw">一文搞定关系数据库设计要领，值得收藏！</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/hd7-IjhRW5DyWZi9MdEG0w">常用数据库SQL命令详解（上）</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/gidYvMFQGM8VaiQkP_9IjQ">常用数据库SQL命令详解（下）</a></td>
</tr>
<tr>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/jhej5m8qs0isEzJC-SpLfA">输入SQL到返回数据，到底发生了什么？</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/M9PmFMmXToJOMyzVWj0Z5g">一条SQL查询语句是如何执行的？</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/Xu_SfM-DzKarybGpspwIHA">数据库主键一定要自增吗？有哪些场景不建议自增？</a></td>
</tr>
<tr>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/2ea_no_oonNb58OqInRn5g">慢SQL，压垮团队的最后一根稻草</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/03lD4P0rE909Kkvm_7vLNg">聊聊数据库建表的15个小技巧</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/FPdl_-jqgeIXSGOh-MQNFQ">百亿级数据分库分表后怎么分页查询？</a></td>
</tr>
<tr>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/PhGDmIumM0nmp8CA6nuzfQ">加密的手机号，如何模糊查询？</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/C1tokV2zikn69KmaJtgU5Q">为什么SQL查询以%开头索引就失效了呢</a></td>
<td align="center"></td>
</tr>
</tbody></table>
<h2 id="怎么保证幂等性"><a href="#怎么保证幂等性" class="headerlink" title="怎么保证幂等性"></a>怎么保证幂等性</h2><h3 id="幂等性的几种方案"><a href="#幂等性的几种方案" class="headerlink" title="幂等性的几种方案"></a>幂等性的几种方案</h3><ol>
<li><p>唯一索引，防止新增脏数据</p>
</li>
<li><p>前端限制:页面的提交按钮只能被点击提交一次<br> 后端解决方案：</p>
<ol>
<li>集群环境：采用token加redis（redis单线程的，处理需要排队）</li>
<li>单JVM环境：采用token加redis或token加jvm内存</li>
<li>处理流程：请求前先生成token，重复代表处理过.数据提交前向授权系统申请token，系统根据相关信息生成token并判断是否可以返回（根据代码逻辑将生成的token与已生成且保存的token做对比，token一致的话代表已经生成了一次，本次不允许返回），将生成的token放到redis或jvm内存，并设置token的有效时间，返回给客户端</li>
</ol>
<p>   <img src="/blog/images/mideng.jpg"><br>    客户端携带token请求服务端，服务端查询redis，如果有的话处理请求并删除token，没有的话代表非法请求不做处理</p>
</li>
</ol>
<blockquote>
<p>现在很多系统处理请求已经不做是否登陆的校验，而是根据用户名密码申请token，将token保存到cookie或者Local Storage或者form隐藏域，请求时携带token，服务端校验处理请求<br><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/X4J56Y2dLzkBVwCC2hlmaQ">Token多平台身份认证架构设计思路</a><br><a target="_blank" rel="noopener" href="https://blog.csdn.net/GreenSky_Test/article/details/116056661">Token登录认证详解</a></p>
</blockquote>
<ol start="3">
<li>悲观锁获取数据的时候加锁获取<pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">select</span> <span class="token operator">*</span> <span class="token keyword">from</span> table_xxx <span class="token keyword">where</span> id<span class="token operator">=</span><span class="token string">'xxx'</span> <span class="token keyword">for</span> <span class="token keyword">update</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>
注意：id字段一定是主键或者唯一索引<br>悲观锁使用时一般伴随事务一起使用，数据锁定时间可能会很长，根据实际情况选用</li>
</ol>
<ul>
<li>乐观锁:乐观锁只是在更新数据那一刻锁表，其他时间不锁表，所以相对于悲观锁，效率更高。乐观锁的实现方式多种多样可以通过version或者其他状态条件：<pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token comment">-- 通过版本号实现</span>
<span class="token keyword">update</span> table_xxx <span class="token keyword">set</span> name<span class="token operator">=</span><span class="token comment">#name#,version=version+1 where version=#version#</span>
<span class="token comment">-- 通过条件限制</span>
<span class="token keyword">update</span> tablexxx <span class="token keyword">set</span> avaiamount<span class="token operator">=</span>avaiamount<span class="token operator">-</span><span class="token comment">#subAmount# where avaiamount-#subAmount# >= 0</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre></li>
</ul>
<ol start="4">
<li><p>分布式锁</p>
</li>
<li><p>状态机幂等<br>在设计单据相关的业务，或者是任务相关的业务，肯定会涉及到状态机(状态变更图)，就是业务单据上面有个状态，状态在不同的情况下会发生变更，一般情况下存在有限状态机,如果状态机已经处于下一个状态，这时候来了一个上一个状态的变更，理论上是不能够变更的，这样的话，保证了有限状态机的幂等。注意：订单等单据类业务，存在很长的状态流转，一定要深刻理解状态机，对业务系统设计能力提高有很大帮助</p>
</li>
<li><p>利用唯一请求编号去重，借用Redis做这个去重——只要这个唯一请求编号在redis存在，证明处理过，那么就认为是重复的</p>
</li>
</ol>
<p><strong>幂等性相关文章</strong></p>
<table>
<thead>
<tr>
<th align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/eVHScVN2kcuZaokzs6-QxA">拒绝接口裸奔！开放API接口签名验证！</a></th>
<th align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/BENMPLwH8WG60UVL5vXj0A">面试问：你的项目是如何处理重复请求&#x2F;并发请求的？</a></th>
<th align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/nza76CX-UJxspSTl52B8eQ">Spring Boot实现接口幂等性的4种方案</a></th>
</tr>
</thead>
<tbody><tr>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/3FJQYVoh_MDXBT0wHoKksQ">高并发下如何保证接口的幂等性？</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/Hd4T8aqrx8gTmkLRyl7hcA">分布式幂等性如何保证</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/Rax8Qb-DrYNpkbc6eTtRlg">消息幂等（去重）通用解决方案，写得真好</a></td>
</tr>
<tr>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/aKStFQXAlFF-1dOax5xg4Q">面试官：给我一个避免消息重复消费的解决方案？</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s?__biz=MzI1NDY0MTkzNQ==&amp;mid=2247497775&amp;idx=1&amp;sn=02f389bfbdb280314e4db32660cd5bc5&amp;scene=21#wechat_redirect">处理接口幂等性的两种常见方案,手把手教你</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/ryhX9usir_d-O8eJuItWzA">如何防止订单重复支付？</a></td>
</tr>
<tr>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/aQwcHv36y0Du4w9HxT5FSw">保证接口数据安全的10种方案</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/2bhhEM8UG8AgIga0RxpW3g">并发扣款，如何保证一致性</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/bktHm67TBWnNi6-xsM3-NA">一种非侵入式幂等性的Java实现</a></td>
</tr>
<tr>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/6WIlEnyYtpO50hAsO4EFNg">如何防止接口重复提交？（上）</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/RrMGoR4DgC7esfWjsX0-Lg">如何防止接口重复提交？（中）</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/Wedb1MyybIXdQEp5xvnxLw">如何防止接口重复提交？（下）</a></td>
</tr>
<tr>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/3iQB9mx550lLIbfUIs4Hkg">如何防止被恶意刷接口？</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/qhCtg-7MSBrin3R0z6286w">SpringBoot实现接口防抖的几种方案，杜绝重复提交</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/JMANcHZohacjFWXFgt7BIA">接口幂等性设计：6种解决方法让重复请求不再成为系统隐患</a></td>
</tr>
</tbody></table>
<h3 id="微服务系统如何设计一个安全的API"><a href="#微服务系统如何设计一个安全的API" class="headerlink" title="微服务系统如何设计一个安全的API"></a>微服务系统如何设计一个安全的API</h3><ol>
<li>身份认证问题<br>一般情况下服务端均会向客户端颁发appId、partnerId等类似于标识用户身份的唯一ID，此ID关联用户密钥，一旦服务端异常或者受到工具，可以追溯来源，调整ID状态或者来强制下线用户</li>
<li>信息泄露问题<br>信息泄露主要是要控制报文在网络传输中不要明文传输，根据对称和非对称加密算法的特点，目前主流的做法是使用混合加密，主要流程是，服务端创建RSA密钥对，将公钥传输给客户端，同时客户端创建AES密钥，使用AES密钥加密明文得到密文，接着使用公钥加密AES密钥，最后将加密后的AES密钥和密文传输到服务端。服务端使用自己的私钥解密加密后的AES密钥得到AES密钥，接着使用AES密钥解密密文得到明文</li>
<li>请求被篡改问题<br>防止请求被篡改主要是要做好加签和验签</li>
<li>重放攻击问题<br>黑客监听到请求后，重复请求攻击服务端，服务端如何识别是非法请求<br>在请求参数中增加timestamp、randomString参数【此参数是从服务端实时请求的】，服务端在接收到请求后timestamp时间戳和服务端相差1分中之内的才放行，接着判断randomString是否已经存在，如果存在则不响应</li>
</ol>
<h3 id="秒杀"><a href="#秒杀" class="headerlink" title="秒杀"></a>秒杀</h3><p><strong>步骤</strong></p>
<p>写脚本肯定需要知道步骤是什么，然后才能用代码去复刻下来嘛。</p>
<p>1、下载浏览器驱动，这里我用的是chrome浏览器，先看一下自己的版本号，在设置可以看到。</p>
<p>然后在<a target="_blank" rel="noopener" href="http://chromedriver.storage.googleapis.com/index.html">网站</a>找好对应的版本去下载</p>
<p>2、接下来就是设置秒杀时间</p>
<p>3、打开浏览器输入淘宝网址</p>
<p>4、登录账号，进入购物车页面</p>
<p>5、点击选择按钮</p>
<p>6、秒杀时间到了，立刻下单！</p>
<p><strong>操作开始</strong></p>
<p>导入依赖：</p>
<pre class="line-numbers language-markup" data-language="markup"><code class="language-markup"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">></span></span>
      <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>org.seleniumhq.selenium<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span>
      <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>selenium-java<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span>
      <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>version</span><span class="token punctuation">></span></span>3.141.59<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>version</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>下面是完整的代码</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">taoBao</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">Exception</span> <span class="token punctuation">&#123;</span>
    <span class="token comment">// 浏览器驱动路径</span>
    <span class="token class-name">System</span><span class="token punctuation">.</span><span class="token function">setProperty</span><span class="token punctuation">(</span><span class="token string">"webdriver.chrome.driver"</span><span class="token punctuation">,</span><span class="token string">"D:\\JDK\\chromedriver.exe"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">// 设置秒杀时间</span>
    <span class="token class-name">SimpleDateFormat</span> sdf <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">SimpleDateFormat</span><span class="token punctuation">(</span><span class="token string">"yyyy-MM-dd HH:mm:ss SSSSSSSSS"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token class-name">Date</span> date <span class="token operator">=</span> sdf<span class="token punctuation">.</span><span class="token function">parse</span><span class="token punctuation">(</span><span class="token string">"2022-04-14 14:07:00 000000000"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">// 1、打开浏览器</span>
    <span class="token class-name">ChromeDriver</span> browser <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ChromeDriver</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token class-name">Actions</span> actions <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Actions</span><span class="token punctuation">(</span>browser<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">// 2、输入网址</span>
    browser<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string">"https://www.taobao.com"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token class-name">Thread</span><span class="token punctuation">.</span><span class="token function">sleep</span><span class="token punctuation">(</span><span class="token number">3000</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">// 3、点击登录</span>
    browser<span class="token punctuation">.</span><span class="token function">findElement</span><span class="token punctuation">(</span><span class="token class-name">By</span><span class="token punctuation">.</span><span class="token function">linkText</span><span class="token punctuation">(</span><span class="token string">"亲，请登录"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">click</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token class-name">Thread</span><span class="token punctuation">.</span><span class="token function">sleep</span><span class="token punctuation">(</span><span class="token number">2000</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">// 4、扫码登录</span>
    browser<span class="token punctuation">.</span><span class="token function">findElement</span><span class="token punctuation">(</span><span class="token class-name">By</span><span class="token punctuation">.</span><span class="token function">className</span><span class="token punctuation">(</span><span class="token string">"icon-qrcode"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">click</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token class-name">Thread</span><span class="token punctuation">.</span><span class="token function">sleep</span><span class="token punctuation">(</span><span class="token number">4000</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">// 5、进入购物车页面</span>
    browser<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string">"https://cart.taobao.com/cart.htm"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token class-name">Thread</span><span class="token punctuation">.</span><span class="token function">sleep</span><span class="token punctuation">(</span><span class="token number">3000</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">// 6、点击选择第一个按钮</span>
    browser<span class="token punctuation">.</span><span class="token function">findElement</span><span class="token punctuation">(</span><span class="token class-name">By</span><span class="token punctuation">.</span><span class="token function">xpath</span><span class="token punctuation">(</span><span class="token string">"//*[@id=\"J_Order_s_2207407355826_1\"]/div[1]/div/div/label"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">click</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token class-name">Thread</span><span class="token punctuation">.</span><span class="token function">sleep</span><span class="token punctuation">(</span><span class="token number">2000</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">while</span> <span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
        <span class="token comment">//当前时间</span>
        <span class="token class-name">Date</span> now <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Date</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>now<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">if</span><span class="token punctuation">(</span>now<span class="token punctuation">.</span><span class="token function">after</span><span class="token punctuation">(</span>date<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
            <span class="token keyword">if</span><span class="token punctuation">(</span>browser<span class="token punctuation">.</span><span class="token function">findElement</span><span class="token punctuation">(</span><span class="token class-name">By</span><span class="token punctuation">.</span><span class="token function">linkText</span><span class="token punctuation">(</span><span class="token string">"结 算"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">isEnabled</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>
                browser<span class="token punctuation">.</span><span class="token function">findElement</span><span class="token punctuation">(</span><span class="token class-name">By</span><span class="token punctuation">.</span><span class="token function">linkText</span><span class="token punctuation">(</span><span class="token string">"结 算"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">click</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"结算成功"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token keyword">break</span><span class="token punctuation">;</span>
            <span class="token punctuation">&#125;</span>

        <span class="token punctuation">&#125;</span>
    <span class="token punctuation">&#125;</span>
    <span class="token class-name">Thread</span><span class="token punctuation">.</span><span class="token function">sleep</span><span class="token punctuation">(</span><span class="token number">5000</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>这里说一下会遇到的问题：</p>
<ol>
<li>这里使用的是扫码登录，需要用手机淘宝扫码进行登录</li>
<li>Thread.sleep(4000);就是系统休息4秒钟，如果扫码登录时间大于4秒会报错，可以根据电脑网速来设置</li>
<li>browser.findElement(By.xpath(“xxx”)).click();这个是选择购物车第一个商家的所有商品，里面xxx需要更改。当然其他参数怎么修改可以根据这个对应来修改。</li>
</ol>
<p>进入购物车页面后按F12，然后点左上角那个箭头，然后选择店铺左边的按钮，这样下面代码块就对应到了指定的代码位置</p>
<p><img src="https://img-blog.csdnimg.cn/b2f3301c394e498eaa8fb7bb87477317.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA6L6w5YWuaW5n,size_20,color_FFFFFF,t_70,g_se,x_16" alt="图片"></p>
<p>右键这一行，然后选择copy→Copy XPath，这个XPath就是browser.findElement(By.xpath(“xxx”)).click();的xxx内容</p>
<p><img src="https://img-blog.csdnimg.cn/c816562669b74a729443ff134c5f0b07.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA6L6w5YWuaW5n,size_20,color_FFFFFF,t_70,g_se,x_16" alt="图片"></p>
<p>如果以上操作都没有问题，那么你就可以启动程序啦！！成功后你会发现，脚本居然如此简单！！</p>
<table>
<thead>
<tr>
<th align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/EYx78REElJE3ASm1PCG2aw">面试官：你给我画一下秒杀系统的架构图</a></th>
<th align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/Uq0zM_ceoFupYDKqqpvR4Q">秒杀场景的九个细节，细思极恐！</a></th>
<th align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/2LpNQTmSFu0InYnC-Q7bJA">秒杀系统设计最全攻略</a></th>
</tr>
</thead>
<tbody><tr>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/YHhbonAxNntonTo-UhDV6Q">下次二面再回答不好“秒杀系统“设计原理，我就捶死自己…</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/AtXswrVQPGD_jfSqzKe_qQ">如何设计订单系统</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/KmPY2NOfpftFi6TpVSW23Q">千万级高并发秒杀系统设计套路</a></td>
</tr>
</tbody></table>
<h2 id="延时队列相关文章"><a href="#延时队列相关文章" class="headerlink" title="延时队列相关文章"></a>延时队列相关文章</h2><table>
<thead>
<tr>
<th align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/ijv_4_qWTrG-kA9jSbz1iw">生成订单30分钟未支付，则自动取消，该怎么实现?</a></th>
<th align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/VcbZTsoD5-ioc4x7s6mj7Q">一口气说出6种实现延时消息的方案</a></th>
<th align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/Oc188nkq4-s9ivt7Ki_M1A">面试官：怎么不用定时任务实现关闭订单？</a></th>
</tr>
</thead>
<tbody><tr>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/BG1PqUWX0XwJX6aMCXCgvw">再有人问你如何实现订单到期关闭，就把这篇文章发给他</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/pGZj1jVHKPhUJKlKNnm5CQ">订单超时怎么处理？阿里用这种方案</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/xQwOdiDGfTelXJQUp0id-g">怎么实现订单30分钟未支付自动取消？我有5种实现方案！</a></td>
</tr>
</tbody></table>
<h2 id="反射"><a href="#反射" class="headerlink" title="反射"></a>反射</h2><h3 id="何为反射"><a href="#何为反射" class="headerlink" title="何为反射"></a>何为反射</h3><p>如果说大家研究过框架的底层原理或者咱们自己写过框架的话，一定对反射这个概念不陌生。反射之所以被称为框架的灵魂，主要是因为它赋予了我们在运行时分析类以及执行类中方法的能力。通过反射你可以获取任意一个类的所有属性和方法，你还可以调用这些方法和属性。</p>
<h3 id="反射的应用场景"><a href="#反射的应用场景" class="headerlink" title="反射的应用场景"></a>反射的应用场景</h3><p>像咱们平时大部分时候都是在写业务代码，很少会接触到直接使用反射机制的场景。但是，这并不代表反射没有用。相反，正是因为反射，你才能这么轻松地使用各种框架。像Spring&#x2F;SpringBoot、MyBatis等等框架中都大量使用了反射机制。这些框架中也大量使用了动态代理，而动态代理的实现也依赖反射。比如下面是通过JDK实现动态代理的示例代码，其中就使用了反射类<code>Method</code>来调用指定的方法。</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">DebugInvocationHandler</span> <span class="token keyword">implements</span> <span class="token class-name">InvocationHandler</span> <span class="token punctuation">&#123;</span>
    <span class="token comment">/**
     * 代理类中的真实对象
     */</span>
    <span class="token keyword">private</span> <span class="token keyword">final</span> <span class="token class-name">Object</span> target<span class="token punctuation">;</span>

    <span class="token keyword">public</span> <span class="token class-name">DebugInvocationHandler</span><span class="token punctuation">(</span><span class="token class-name">Object</span> target<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>target <span class="token operator">=</span> target<span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span>


    <span class="token keyword">public</span> <span class="token class-name">Object</span> <span class="token function">invoke</span><span class="token punctuation">(</span><span class="token class-name">Object</span> proxy<span class="token punctuation">,</span> <span class="token class-name">Method</span> method<span class="token punctuation">,</span> <span class="token class-name">Object</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">InvocationTargetException</span><span class="token punctuation">,</span> <span class="token class-name">IllegalAccessException</span> <span class="token punctuation">&#123;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"before method "</span> <span class="token operator">+</span> method<span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">Object</span> result <span class="token operator">=</span> method<span class="token punctuation">.</span><span class="token function">invoke</span><span class="token punctuation">(</span>target<span class="token punctuation">,</span> args<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"after method "</span> <span class="token operator">+</span> method<span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> result<span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>另外，像Java中的一大利器注解的实现也用到了反射。为什么你使用Spring的时候，一个@Component注解就声明了一个类为Spring Bean呢？为什么你通过一个@Value注解就读取到配置文件中的值呢？究竟是怎么起作用的呢？这些都是因为你可以基于反射分析类，然后获取到类&#x2F;属性&#x2F;方法&#x2F;方法的参数上的注解。你获取到注解之后，就可以做进一步的处理。</p>
<h3 id="反射机制的优缺点"><a href="#反射机制的优缺点" class="headerlink" title="反射机制的优缺点"></a>反射机制的优缺点</h3><p><strong>优点</strong>：可以让咱们的代码更加灵活、为各种框架提供开箱即用的功能提供了便利</p>
<p><strong>缺点</strong>：让我们在运行时有了分析操作类的能力，这同样也增加了安全问题。比如可以无视泛型参数的安全检查（泛型参数的安全检查发生在编译时）。另外，反射的性能也要稍差点，不过，对于框架来说实际是影响不大的。</p>
<blockquote>
<p>相关阅读：<a target="_blank" rel="noopener" href="https://stackoverflow.com/questions/1392351/java-reflection-why-is-it-so-slow">Java Reflection:Why is it so slow?</a><br><a target="_blank" rel="noopener" href="https://javaguide.cn/java/basis/reflection.html">原文链接</a></p>
</blockquote>
<h3 id="反射相关文章"><a href="#反射相关文章" class="headerlink" title="反射相关文章"></a>反射相关文章</h3><table>
<thead>
<tr>
<th align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/jV9kE2ajB40f3fOU_lT9ng">Java反射机制你还不会？那怎么看Spring源码？</a></th>
<th align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/QbacsQwTyvBJi12LYPNKJw">Java反射是什么？看这篇绝对会了！</a></th>
<th align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/Dyg4qSqiyjSJTne8yvUYpQ">学会这篇反射，我就可以去吹牛逼了。</a></th>
</tr>
</thead>
<tbody><tr>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/kTYLjg_FlKBdAAQQvSAF9g">深入理解Java：类加载机制及反射</a></td>
<td align="center"></td>
<td align="center"></td>
</tr>
</tbody></table>
<h2 id="安卓ROOT大法"><a href="#安卓ROOT大法" class="headerlink" title="安卓ROOT大法"></a>安卓ROOT大法</h2><h3 id="线刷"><a href="#线刷" class="headerlink" title="线刷"></a>线刷</h3><ol>
<li>电脑可以使用fastboot、adb等命令</li>
<li>下载magisk</li>
<li>解锁BL</li>
<li>提取boot.img</li>
<li>使用magisk制作boot.img，将生成的文件拷贝到电脑，生成路径一般在download文件夹下</li>
<li>进入fastboot模式（一般在关机状态下长按音量减+电源键）</li>
<li>使用<code>fastboot flash boot magisk制作的img文件路径.img</code></li>
<li>使用<code>fastboot reboot</code>重启</li>
<li>完成</li>
</ol>
<h3 id="卡刷"><a href="#卡刷" class="headerlink" title="卡刷"></a>卡刷</h3><ol>
<li>安装第三方recovery如twrp，镜像文件为recovery.img</li>
<li>手机进入fastboot模式，使用<code>fastboot flash recovery recovery.img</code>并重启手机</li>
<li>将magisk.apk重命名为magisk.zip</li>
</ol>
<h3 id="链接"><a href="#链接" class="headerlink" title="链接"></a>链接</h3><table>
<thead>
<tr>
<th align="center"><a target="_blank" rel="noopener" href="https://www.bilibili.com/video/BV1BY4y1H7Mc/">玩机必看！带你入坑安卓刷机，小白也能看懂的ROOT基础指南来啦！</a></th>
<th align="center"><a target="_blank" rel="noopener" href="http://wanji.jamcz.com/">各种玩机地址&#x2F;工具&#x2F;命令汇总</a></th>
<th><a target="_blank" rel="noopener" href="https://developer.android.google.cn/studio/releases/platform-tools?hl=zh-cn">Android SDK（ADB和Fastboot电脑端工具）</a></th>
</tr>
</thead>
<tbody><tr>
<td align="center"><a target="_blank" rel="noopener" href="https://cz-jam.lanzouj.com/iZICY02v2k8j">ADB&#x2F;Fastboot驱动</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://twrp.me/">TWRP</a></td>
<td><a target="_blank" rel="noopener" href="https://github.com/topjohnwu/Magisk">Magisk</a></td>
</tr>
<tr>
<td align="center"><a target="_blank" rel="noopener" href="https://www.miui.com/unlock/index.html">小米解锁工具</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://xiaomirom.com/series/">小米ROM下载</a></td>
<td><a target="_blank" rel="noopener" href="https://cdn.alsgp0.fds.api.mi-img.com/micomm/MiFlash2020-3-14-0.rar">小米刷机工具</a></td>
</tr>
<tr>
<td align="center"><a target="_blank" rel="noopener" href="https://www.oneplus.com/cn/support/softwareupgrade">一加ROM下载</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://odindownload.com/">三星刷机工具</a></td>
<td><a target="_blank" rel="noopener" href="https://www.sammobile.com/firmwares/">三星ROM下载</a></td>
</tr>
<tr>
<td align="center"><a target="_blank" rel="noopener" href="https://mroot.flyme.cn/">魅族ROOT链接</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://www.oppo.cn/thread-397164526-1">OPPO解锁</a></td>
<td><a target="_blank" rel="noopener" href="https://www.coloros.com/rom">OPPO ROM</a></td>
</tr>
<tr>
<td align="center"><a target="_blank" rel="noopener" href="https://www.realmebbs.com/post-details/1275426081138028544">Realme解锁</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://www.realmebbs.com/post-details/1271082970484060160">Realme刷机工具</a></td>
<td><a target="_blank" rel="noopener" href="https://www.realme.com/support/software-update">Realme ROM</a></td>
</tr>
<tr>
<td align="center"><a target="_blank" rel="noopener" href="https://developer.sony.com/develop/open-devices/get-started/unlock-bootloader">索尼解锁</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://xperifirm.com/">索尼ROM</a></td>
<td><a target="_blank" rel="noopener" href="https://motorola-global-portal.custhelp.com/app/standalone/bootloader/unlock-your-device-a">MOTO解锁</a></td>
</tr>
<tr>
<td align="center"><a target="_blank" rel="noopener" href="https://mirrors.lolinet.com/firmware/motorola/">MOTO ROM</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://mrzzoxo.lanzouw.com/iR65zpaueyd">payload-dumper解包工具</a></td>
<td><a target="_blank" rel="noopener" href="https://www.coolapk.com/">酷安（玩机社区）</a></td>
</tr>
<tr>
<td align="center"><a target="_blank" rel="noopener" href="https://forum.xda-developers.com/">XDA（海外玩机论坛）</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://www.meizubl.com/">魅族BL解锁</a></td>
<td><a target="_blank" rel="noopener" href="https://orangefox.download/zh-CN">橙狐</a></td>
</tr>
</tbody></table>
<h3 id="注意事项"><a href="#注意事项" class="headerlink" title="注意事项"></a>注意事项</h3><p>magisk如果收不到其他app授权申请弹窗的话授权magisk悬浮窗和自启动权限</p>

                
            </div>
            <hr/>

            

    <div class="reprint" id="reprint-statement">
        
            <div class="reprint__author">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-user">
                        文章作者:
                    </i>
                </span>
                <span class="reprint-info">
                    <a href="/blog/about" rel="external nofollow noreferrer">xmxe</a>
                </span>
            </div>
            <div class="reprint__type">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-link">
                        文章链接:
                    </i>
                </span>
                <span class="reprint-info">
                    <a href="https://xmxe.github.io/blog/posts/8e7bdb140cc5/">https://xmxe.github.io/blog/posts/8e7bdb140cc5/</a>
                </span>
            </div>
            <div class="reprint__notice">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-copyright">
                        版权声明:
                    </i>
                </span>
                <span class="reprint-info">
                    本博客所有文章除特別声明外，均采用
                    <a href="https://creativecommons.org/licenses/by/4.0/deed.zh" rel="external nofollow noreferrer" target="_blank">CC BY 4.0</a>
                    许可协议。转载请注明来源
                    <a href="/blog/about" target="_blank">xmxe</a>
                    !
                </span>
            </div>
        
    </div>

    <script async defer>
      document.addEventListener("copy", function (e) {
        let toastHTML = '<span>复制成功，请遵循本文的转载规则</span><button class="btn-flat toast-action" onclick="navToReprintStatement()" style="font-size: smaller">查看</a>';
        M.toast({html: toastHTML})
      });

      function navToReprintStatement() {
        $("html, body").animate({scrollTop: $("#reprint-statement").offset().top - 80}, 800);
      }
    </script>



            <div class="tag_share" style="display: block;">
                <div class="post-meta__tag-list" style="display: inline-block;">
                    
                        <div class="article-tag">
                            
                                <a href="/blog/tags/%E9%9A%8F%E7%AC%94/">
                                    <span class="chip bg-color">随笔</span>
                                </a>
                            
                        </div>
                    
                </div>
                <div class="post_share" style="zoom: 80%; width: fit-content; display: inline-block; float: right; margin: -0.15rem 0;">
                    <link rel="stylesheet" type="text/css" href="/blog/libs/share/css/share.min.css">
<div id="article-share">

    
    <div class="social-share" data-sites="qq,qzone,wechat,weibo,douban" data-wechat-qrcode-helper="<p>微信扫一扫即可分享！</p>"></div>
    <script src="/blog/libs/share/js/social-share.min.js"></script>
    

    

</div>

                </div>
            </div>
            
                <style>
    #reward {
        margin: 40px 0;
        text-align: center;
    }

    #reward .reward-link {
        font-size: 1.4rem;
        line-height: 38px;
    }

    #reward .btn-floating:hover {
        box-shadow: 0 6px 12px rgba(0, 0, 0, 0.2), 0 5px 15px rgba(0, 0, 0, 0.2);
    }

    #rewardModal {
        width: 320px;
        height: 350px;
    }

    #rewardModal .reward-title {
        margin: 15px auto;
        padding-bottom: 5px;
    }

    #rewardModal .modal-content {
        padding: 10px;
    }

    #rewardModal .close {
        position: absolute;
        right: 15px;
        top: 15px;
        color: rgba(0, 0, 0, 0.5);
        font-size: 1.3rem;
        line-height: 20px;
        cursor: pointer;
    }

    #rewardModal .close:hover {
        color: #ef5350;
        transform: scale(1.3);
        -moz-transform:scale(1.3);
        -webkit-transform:scale(1.3);
        -o-transform:scale(1.3);
    }

    #rewardModal .reward-tabs {
        margin: 0 auto;
        width: 210px;
    }

    .reward-tabs .tabs {
        height: 38px;
        margin: 10px auto;
        padding-left: 0;
    }

    .reward-content ul {
        padding-left: 0 !important;
    }

    .reward-tabs .tabs .tab {
        height: 38px;
        line-height: 38px;
    }

    .reward-tabs .tab a {
        color: #fff;
        background-color: #ccc;
    }

    .reward-tabs .tab a:hover {
        background-color: #ccc;
        color: #fff;
    }

    .reward-tabs .wechat-tab .active {
        color: #fff !important;
        background-color: #22AB38 !important;
    }

    .reward-tabs .alipay-tab .active {
        color: #fff !important;
        background-color: #019FE8 !important;
    }

    .reward-tabs .reward-img {
        width: 210px;
        height: 210px;
    }
</style>

<div id="reward">
    <a href="#rewardModal" class="reward-link modal-trigger btn-floating btn-medium waves-effect waves-light red">赏</a>

    <!-- Modal Structure -->
    <div id="rewardModal" class="modal">
        <div class="modal-content">
            <a class="close modal-close"><i class="fas fa-times"></i></a>
            <h4 class="reward-title">你的赏识是我前进的动力</h4>
            <div class="reward-content">
                <div class="reward-tabs">
                    <ul class="tabs row">
                        <li class="tab col s6 alipay-tab waves-effect waves-light"><a href="#alipay">支付宝</a></li>
                        <li class="tab col s6 wechat-tab waves-effect waves-light"><a href="#wechat">微 信</a></li>
                    </ul>
                    <div id="alipay">
                        
                            <img src="/blog/medias/reward/alipay.jpg" class="reward-img" alt="支付宝打赏二维码">
                        
                    </div>
                    <div id="wechat">
                        
                            <img src="/blog/medias/reward/wechat.jpg" class="reward-img" alt="微信打赏二维码">
                        
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    $(function () {
        $('.tabs').tabs();
    });
</script>

            
        </div>
    </div>

    

    

    

    

    

    

    

    

<article id="prenext-posts" class="prev-next articles">
    <div class="row article-row">
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge left-badge text-color">
                <i class="fas fa-chevron-left"></i>&nbsp;上一篇</div>
            <div class="card">
                <a href="/blog/posts/f5d8fbdfd125/">
                    <div class="card-image">
                        
                        <img src="https://picx1.zhimg.com/v2-ff627d205bd26ab15e4bdefd5daf1dda_1440w.jpg" class="responsive-img" alt="JavaScript相关">
                        
                        <span class="card-title">JavaScript相关</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                            NoteJQuery、JS常用方法// json字符串
var str1 = '&#123;"name":"cxh","sex":"man"&#125;';
// json对象
var str2 = &#123;name:"cxh",sex
                        

                    </div>
                    <div class="publish-info">
                        <span class="publish-date">
                            <!--
                            <i class="far fa-clock fa-fw icon-date"></i>2022-09-29
                            -->

                            
                                <i class="fas fa-user fa-fw"></i>
                                <a href="/blog/about" >
                                    xmxe
                                </a>
                            
                        </span>
                        <span class="publish-author">
                            
                                <i class="fas fa-bookmark fa-fw icon-category"></i>
                                
                                    <a href="/blog/categories/JS/" class="post-category">
                                        JS
                                    </a>
                                
                            
                                
                                
                                <a href="/blog/tags/%E4%BB%A3%E7%A0%81%E5%AE%9E%E6%88%98/">
                                    <span class="chip bg-color">代码实战</span>
                                </a>
                                
                                
                            

                        </span>
                    </div>
                </div>
                
            </div>
        </div>
        
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge right-badge text-color">
                下一篇&nbsp;<i class="fas fa-chevron-right"></i>
            </div>
            <div class="card">
                <a href="/blog/posts/442dc7ae24f1/">
                    <div class="card-image">
                        
                        <img src="https://img1.baidu.com/it/u=1369410832,3000356447&fm=253&fmt=auto&app=138&f=PNG?w=500&h=539" class="responsive-img" alt="消息队列-MQ">
                        
                        <span class="card-title">消息队列-MQ</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                            什么是消息队列我们可以把消息队列看作是一个存放消息的容器，当我们需要使用消息的时候，直接从容器中取出消息供自己使用即可。由于队列Queue是一种先进先出的数据结构，所以消费消息时也是按照顺序来消费的。

参与消息传递的双方称为生产者和消费者
                        
                        
                    </div>
                    <div class="publish-info">
                        <span class="publish-date">
                            <!--
                            <i class="far fa-clock fa-fw icon-date"></i>2022-09-29
                            -->
                            
                                <i class="fas fa-user fa-fw"></i>
                                <a href="/blog/about" >
                                    xmxe
                                </a>
                            
                        </span>
                        <span class="publish-author">
                            
                                <i class="fas fa-bookmark fa-fw icon-category"></i>
                                
                                    <a href="/blog/categories/%E6%8A%80%E6%9C%AF%E6%A0%88/" class="post-category">
                                        技术栈
                                    </a>
                                
                            
                        </span>
                    </div>
                </div>
                
            </div>
        </div>
        
    </div>
</article>

</div>



<!-- 代码块功能依赖 -->
<script type="text/javascript" src="/blog/libs/codeBlock/codeBlockFuction.js"></script>

<!-- 代码语言 -->

<script type="text/javascript" src="/blog/libs/codeBlock/codeLang.js"></script>


<!-- 代码块复制 -->

<script type="text/javascript" src="/blog/libs/codeBlock/codeCopy.js"></script>


<!-- 代码块收缩 -->

<script type="text/javascript" src="/blog/libs/codeBlock/codeShrink.js"></script>


    </div>
    <div id="toc-aside" class="expanded col l3 hide-on-med-and-down">
        <div class="toc-widget card">
            <div class="toc-title"><i class="far fa-list-alt"></i>&nbsp;&nbsp;目录</div>
            <div id="toc-content"></div>
        </div>
    </div>
</div>

<!-- TOC 悬浮按钮. -->

<div id="floating-toc-btn" class="hide-on-med-and-down">
    <a class="btn-floating btn-large bg-color">
        <i class="fas fa-list-ul"></i>
    </a>
</div>


<script src="/blog/libs/tocbot/tocbot.min.js"></script>
<script>
    $(function () {
        tocbot.init({
            tocSelector: '#toc-content',
            contentSelector: '#articleContent',
            headingsOffset: -($(window).height() * 0.4 - 45),
            collapseDepth: Number('0'),
            headingSelector: 'h1, h2, h3, h4, h5, h6'
        });

        // modify the toc link href to support Chinese.
        let i = 0;
        let tocHeading = 'toc-heading-';
        $('#toc-content a').each(function () {
            $(this).attr('href', '#' + tocHeading + (++i));
        });

        // modify the heading title id to support Chinese.
        i = 0;
        $('#articleContent').children('h1, h2, h3, h4, h5, h6').each(function () {
            $(this).attr('id', tocHeading + (++i));
        });

        // Set scroll toc fixed.
        let tocHeight = parseInt($(window).height() * 0.4 - 64);
        let $tocWidget = $('.toc-widget');
        $(window).scroll(function () {
            let scroll = $(window).scrollTop();
            /* add post toc fixed. */
            if (scroll > tocHeight) {
                $tocWidget.addClass('toc-fixed');
            } else {
                $tocWidget.removeClass('toc-fixed');
            }
        });

        
        /* 修复文章卡片 div 的宽度. */
        let fixPostCardWidth = function (srcId, targetId) {
            let srcDiv = $('#' + srcId);
            if (srcDiv.length === 0) {
                return;
            }

            let w = srcDiv.width();
            if (w >= 450) {
                w = w + 21;
            } else if (w >= 350 && w < 450) {
                w = w + 18;
            } else if (w >= 300 && w < 350) {
                w = w + 16;
            } else {
                w = w + 14;
            }
            $('#' + targetId).width(w);
        };

        // 切换TOC目录展开收缩的相关操作.
        const expandedClass = 'expanded';
        let $tocAside = $('#toc-aside');
        let $mainContent = $('#main-content');
        $('#floating-toc-btn .btn-floating').click(function () {
            if ($tocAside.hasClass(expandedClass)) {
                $tocAside.removeClass(expandedClass).hide();
                $mainContent.removeClass('l9');
            } else {
                $tocAside.addClass(expandedClass).show();
                $mainContent.addClass('l9');
            }
            fixPostCardWidth('artDetail', 'prenext-posts');
        });
        
    });
</script>

    

</main>




    <footer class="page-footer bg-color">
    
        <link rel="stylesheet" href="/blog/libs/aplayer/APlayer.min.css">
<style>
    .aplayer .aplayer-lrc p {
        
        display: none;
        
        font-size: 12px;
        font-weight: 700;
        line-height: 16px !important;
    }

    .aplayer .aplayer-lrc p.aplayer-lrc-current {
        
        display: none;
        
        font-size: 15px;
        color: #42b983;
    }

    
    .aplayer.aplayer-fixed.aplayer-narrow .aplayer-body {
        left: -66px !important;
    }

    .aplayer.aplayer-fixed.aplayer-narrow .aplayer-body:hover {
        left: 0px !important;
    }

    
</style>
<div class="">
    
    <div class="row">
        <meting-js class="col l8 offset-l2 m10 offset-m1 s12"
                   server="netease"
                   type="song"
                   id="569200213"
                   fixed='true'
                   autoplay='false'
                   theme='#42b983'
                   loop='all'
                   order='random'
                   preload='auto'
                   volume='0.7'
                   list-folded='true'
        >
        </meting-js>
    </div>
</div>

<script src="/blog/libs/aplayer/APlayer.min.js"></script>
<!-- <script src="https://cdn.jsdelivr.net/npm/meting@2/dist/Meting.min.js"></script> -->

    
    <div class="container row center-align" style="margin-bottom: 0px !important;">
        <div class="col s12 m8 l8 copy-right">
            Copyright&nbsp;&copy;
            
            
                <span id="year">2022-2025
                </span>
            
            

            <a href="/blog/about" target="_blank">
                xmxe
            </a>
            |&nbsp;Powered by&nbsp;
            <a href="https://hexo.io/" target="_blank">Hexo</a>
            |&nbsp;Theme&nbsp;
            <a href="https://github.com/blinkfox/hexo-theme-matery" target="_blank">Matery</a>
            <br>

            
            
            
            
            
            

            
            <br>

            
            <br>

            
        </div>
        <div class="col s12 m4 l4 social-link2 ">
    <a href="https://github.com/xmxe" class="tooltipped" target="_blank" data-tooltip="GitHub" data-position="top" data-delay="50">
        <i class="fab fa-github"></i>
    </a>



    <a href="https://gitee.com/xmxe" class="tooltipped" target="_blank" data-tooltip="码云" data-position="top" data-delay="50">
        <svg width="19" height="19" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" style="position: relative; top: 2px; left: 0.5px;">
            <path d="M512 1024C230.4 1024 0 793.6 0 512S230.4 0 512 0s512 230.4 512 512-230.4 512-512 512z m259.2-569.6H480c-12.8 0-25.6 12.8-25.6 25.6v64c0 12.8 12.8 25.6 25.6 25.6h176c12.8 0 25.6 12.8 25.6 25.6v12.8c0 41.6-35.2 76.8-76.8 76.8h-240c-12.8 0-25.6-12.8-25.6-25.6V416c0-41.6 35.2-76.8 76.8-76.8h355.2c12.8 0 25.6-12.8 25.6-25.6v-64c0-12.8-12.8-25.6-25.6-25.6H416c-105.6 0-188.8 86.4-188.8 188.8V768c0 12.8 12.8 25.6 25.6 25.6h374.4c92.8 0 169.6-76.8 169.6-169.6v-144c0-12.8-12.8-25.6-25.6-25.6z" fill="#fff">
            </path>
        </svg>
    </a>

















    
        
          <a href="/blog/download" class="tooltipped" target="_blank" data-tooltip="下载" data-position="top" data-delay="50">
            <i class="fas fa-download"></i>
          </a>
        
    



    <style>
  .mobiledevice {
    display: none !important;
  }

  footer .wechat_qrcode {
    position: fixed;
  }

  /*微信二维码*/
  .wechat_qrcode {
    position: absolute;
    margin-left: 10px;
    bottom: 10px;
    background: url("/blog/medias/xcx.png");
    zoom:40%;
  }

  .wechat:hover .wechat_qrcode {
    width: 430px;
    height: 430px;
    animation: move 0.4s linear 1 normal;
  }

  @keyframes move {
    0% {
      transform: translate(100px, 0);
      opacity: 0;
    }
    50% {
      transform: translate(50px, 0);
      opacity: 0.5;
    }
    100% {
      transform: translate(0, 0);
      opacity: 1;
    }
  }

  @media only screen and (max-width: 601px) {
    .wechat {
      display: none !important;
    }
    .mobiledevice {
      display: inline-block !important;
    }
  }
</style>

<a href="javascript:;" class="wechat" data-position="top" data-delay="50">
  <i class="fab fa-weixin"></i>
  <img class="wechat_qrcode" />
</a>

<a
  href="javascript:;"
  class="tooltipped mobiledevice"
  data-tooltip="微信: 464817304"
  data-position="top"
  data-delay="50"
>
  <i class="fab fa-weixin"></i>
</a>

</div>
    </div>
</footer>

<div class="progress-bar"></div>


    <!-- 搜索遮罩框 -->
<div id="searchModal" class="modal">
    <div class="modal-content">
        <div class="search-header">
            <span class="title"><i class="fas fa-search"></i>&nbsp;&nbsp;搜索</span>
            <input type="search" id="searchInput" name="s" placeholder="请输入搜索的关键字"
                   class="search-input">
        </div>
        <div id="searchResult"></div>
    </div>
</div>

<script type="text/javascript">
$(function () {
    var searchFunc = function (path, search_id, content_id) {
        'use strict';
        $.ajax({
            url: path,
            dataType: "xml",
            success: function (xmlResponse) {
                // get the contents from search data
                var datas = $("entry", xmlResponse).map(function () {
                    return {
                        title: $("title", this).text(),
                        content: $("content", this).text(),
                        url: $("url", this).text()
                    };
                }).get();
                var $input = document.getElementById(search_id);
                var $resultContent = document.getElementById(content_id);
                $input.addEventListener('input', function () {
                    var str = '<ul class=\"search-result-list\">';
                    var keywords = this.value.trim().toLowerCase().split(/[\s\-]+/);
                    $resultContent.innerHTML = "";
                    if (this.value.trim().length <= 0) {
                        return;
                    }
                    // perform local searching
                    datas.forEach(function (data) {
                        var isMatch = true;
                        var data_title = data.title.trim().toLowerCase();
                        var data_content = data.content.trim().replace(/<[^>]+>/g, "").toLowerCase();
                        var data_url = data.url;
                        data_url = data_url.indexOf('/') === 0 ? data.url : '/' + data_url;
                        var index_title = -1;
                        var index_content = -1;
                        var first_occur = -1;
                        // only match artiles with not empty titles and contents
                        if (data_title !== '' && data_content !== '') {
                            keywords.forEach(function (keyword, i) {
                                index_title = data_title.indexOf(keyword);
                                index_content = data_content.indexOf(keyword);
                                if (index_title < 0 && index_content < 0) {
                                    isMatch = false;
                                } else {
                                    if (index_content < 0) {
                                        index_content = 0;
                                    }
                                    if (i === 0) {
                                        first_occur = index_content;
                                    }
                                }
                            });
                        }
                        // show search results
                        if (isMatch) {
                            str += "<li><a href='" + data_url + "' class='search-result-title'>" + data_title + "</a>";
                            var content = data.content.trim().replace(/<[^>]+>/g, "");
                            if (first_occur >= 0) {
                                // cut out 100 characters
                                var start = first_occur - 20;
                                var end = first_occur + 80;
                                if (start < 0) {
                                    start = 0;
                                }
                                if (start === 0) {
                                    end = 100;
                                }
                                if (end > content.length) {
                                    end = content.length;
                                }
                                var match_content = content.substr(start, end);
                                // highlight all keywords
                                keywords.forEach(function (keyword) {
                                    var regS = new RegExp(keyword, "gi");
                                    match_content = match_content.replace(regS, "<em class=\"search-keyword\">" + keyword + "</em>");
                                });

                                str += "<p class=\"search-result\">" + match_content + "...</p>"
                            }
                            str += "</li>";
                        }
                    });
                    str += "</ul>";
                    $resultContent.innerHTML = str;
                });
            }
        });
    };

    searchFunc('/blog/search.xml', 'searchInput', 'searchResult');
});
</script>

    <!-- 回到顶部按钮 -->
<div id="backTop" class="top-scroll">
    <a class="btn-floating btn-large waves-effect waves-light" href="#!">
        <i class="fas fa-arrow-up"></i>
    </a>
</div>

    <div class="stars-con">
  <div id="stars"></div>
  <div id="stars2"></div>
  <div id="stars3"></div>  
</div>

<!-- 白天和黑夜主题 -->



<script>
  function switchNightMode() {
    
      setTimeout(function () {
        $('body').hasClass('DarkMode') 
        ? ($('body').removeClass('DarkMode'), localStorage.setItem('isDark', '0'), $('#sum-moon-icon').removeClass("fa-sun").addClass('fa-moon')) 
        : ($('body').addClass('DarkMode'), localStorage.setItem('isDark', '1'), $('#sum-moon-icon').addClass("fa-sun").removeClass('fa-moon')),
          
        setTimeout(function () {
          $('.Cuteen_DarkSky').fadeOut(1e3, function () {
            $(this).remove()
          })
        }, 2e3)
      })
  }
</script>
    
    
    <script>
        /* 模式判断 */
        if (localStorage.getItem('isDark') === '1') {
            document.body.classList.add('DarkMode');
            $('#sum-moon-icon').addClass("fa-sun").removeClass('fa-moon')
        } else {
            document.body.classList.remove('DarkMode');
            $('#sum-moon-icon').removeClass("fa-sun").addClass('fa-moon')
        }
    </script>

    <script src="/blog/libs/materialize/materialize.min.js"></script>
    <script src="/blog/libs/masonry/masonry.pkgd.min.js"></script>
    <script src="/blog/libs/aos/aos.js"></script>
    <script src="/blog/libs/scrollprogress/scrollProgress.min.js"></script>
    <script src="/blog/libs/lightGallery/js/lightgallery-all.min.js"></script>
    <script src="/blog/js/matery.js"></script>

    <!-- Baidu Analytics -->

    <!-- Baidu Push -->

<script>
    (function () {
        var bp = document.createElement('script');
        var curProtocol = window.location.protocol.split(':')[0];
        if (curProtocol === 'https') {
            bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
        } else {
            bp.src = 'http://push.zhanzhang.baidu.com/push.js';
        }
        var s = document.getElementsByTagName("script")[0];
        s.parentNode.insertBefore(bp, s);
    })();
</script>

    
    

    

    

	
    

    

    
    <script type="text/javascript" src="/blog/libs/background/ribbon-dynamic.js" async="async"></script>
    

    

    <!-- 冒泡 -->
    
    <script type="text/javascript">
        // 只在桌面版网页启用特效
        // var windowWidth = $(window).width();
        
            document.write('<script type="text/javascript" src="/blog/libs/others/bubleAll.js"><\/script>');
        
        
    </script>
    

    <!-- 弹出文字 -->
    

</body>

</html>
